Skip to content

Commit

Permalink
Variadic option arguments for Test and Step functions (#22)
Browse files Browse the repository at this point in the history
* WIP version of function slices.

* WIP for option slices.

* WIP for option slices.

* Working version of variadic function arguments for Test and Step functions.

* Re-arranged functions and interfaces to remove `test` and `steps` packages.

* Changed some non-essential types to private.

* Addressed review comments: renamed allure.Body to allure.Action for more clarity and added allure.Parameters for more convenience.
  • Loading branch information
ubermensch01 authored and MaximeBellou committed Jan 22, 2020
1 parent e94883e commit b778d93
Show file tree
Hide file tree
Showing 22 changed files with 553 additions and 419 deletions.
12 changes: 6 additions & 6 deletions attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ func AddAttachment(name string, mimeType MimeType, content []byte) error {
return errors.Wrap(err, "Failed to create an attachment file")
}
if node, ok := ctxMgr.GetValue(nodeKey); ok {
node.(hasAttachments).AddAttachment(*attachment)
node.(hasAttachments).addAttachment(*attachment)
}

return nil
}

func (a *attachment) writeAttachmentFile() error {
resultsPathEnv := os.Getenv(resultsPathEnvKey)
resultsPathEnv := os.Getenv(ResultsPathEnvKey)
if resultsPathEnv == "" {
log.Printf("%s environment variable cannot be empty\n", resultsPathEnvKey)
log.Printf("%s environment variable cannot be empty\n", ResultsPathEnvKey)
}
if resultsPath == "" {
resultsPath = fmt.Sprintf("%s/allure-results", resultsPathEnv)
if ResultsPath == "" {
ResultsPath = fmt.Sprintf("%s/allure-results", resultsPathEnv)
}

a.Source = fmt.Sprintf("%s-attachment", a.uuid)
err := ioutil.WriteFile(fmt.Sprintf("%s/%s-attachment", resultsPath, a.uuid), a.content, 0777)
err := ioutil.WriteFile(fmt.Sprintf("%s/%s-attachment", ResultsPath, a.uuid), a.content, 0777)
if err != nil {
return errors.Wrap(err, "Failed to write in file")
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func allureError(err error, status string, now bool) {
manipulateOnObjectFromCtx(
nodeKey,
func(node interface{}) {
node.(hasStatus).SetStatus(status)
node.(hasStatus).setStatus(status)
})
manipulateOnObjectFromCtx(
testInstanceKey,
Expand Down
24 changes: 12 additions & 12 deletions example/example_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,37 @@ import (
)

func TestTextAttachmentToStep(t *testing.T) {
allure.Test(t, "Testing a text attachment", func() {
allure.Step("adding a text attachment", func() {
allure.Test(t, allure.Description("Testing a text attachment"), allure.Action(func() {
allure.Step(allure.Description("adding a text attachment"), allure.Action(func() {
_ = allure.AddAttachment("text!", allure.TextPlain, []byte("Some text!"))
})
})
}))
}))
}

func TestImageAttachmentToStep(t *testing.T) {
allure.Test(t, "testing an image attachment", func() {
allure.Step("adding an image attachment", func() {
allure.Test(t, allure.Description("testing an image attachment"), allure.Action(func() {
allure.Step(allure.Description("adding an image attachment"), allure.Action(func() {
dat, err := ioutil.ReadFile("../Coryphaena_hippurus.png")
if err != nil {
log.Println(err)
}
_ = allure.AddAttachment("mahi-mahi", allure.ImagePng, dat)
})
})
}))
}))
}

func TestTextAttachment(t *testing.T) {
allure.Test(t, "Testing a text attachment", func() {
allure.Test(t, allure.Description("Testing a text attachment"), allure.Action(func() {
_ = allure.AddAttachment("text!", allure.TextPlain, []byte("Some text!"))
})
}))
}

func TestImageAttachment(t *testing.T) {
allure.Test(t, "testing an image attachment", func() {
allure.Test(t, allure.Description("testing an image attachment"), allure.Action(func() {
dat, err := ioutil.ReadFile("../Coryphaena_hippurus.png")
if err != nil {
log.Println(err)
}
_ = allure.AddAttachment("mahi-mahi", allure.ImagePng, dat)
})
}))
}
35 changes: 17 additions & 18 deletions example/example_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,34 @@ import (
)

func TestFailAllure(t *testing.T) {
allure.Test(t, "Test with Allure error in it", func() {
allure.Step("Step 1", func() {
})
allure.Step("Step 2", func() {
allure.Test(t, allure.Description("Test with Allure error in it"), allure.Action(func() {
allure.Step(allure.Description("Step 1"), allure.Action(func() {
}))
allure.Step(allure.Description("Step 2"), allure.Action(func() {
allure.Fail(errors.New("Error message"))
})
})
}))
}))
}

func TestFailNowAllure(t *testing.T) {
allure.Test(t, "Test with Allure error in it", func() {
allure.Test(t, allure.Description("Test with Allure error in it"), allure.Action(func() {
allure.FailNow(errors.New("A more serious error"))
allure.Step("Step you're not supposed to see", func() {})
})
allure.Step(allure.Description("Step you're not supposed to see"), allure.Action(func() {}))
}))
}

func TestBreakAllure(t *testing.T) {
allure.Test(t, "Test with Allure error in it", func() {
allure.Step("Step 1", func() {
})
allure.Step("Step 2", func() {
allure.Test(t, allure.Description("Test with Allure error in it"), allure.Action(func() {
allure.Step(allure.Description("Step 1"), allure.Action(func() {}))
allure.Step(allure.Description("Step 2"), allure.Action(func() {
allure.Break(errors.New("Error message"))
})
})
}))
}))
}

func TestBreakNowAllure(t *testing.T) {
allure.Test(t, "Test with Allure error in it", func() {
allure.Test(t, allure.Description("Test with Allure error in it"), allure.Action(func() {
allure.BreakNow(errors.New("A more serious error"))
allure.Step("Step you're not supposed to see", func() {})
})
allure.Step(allure.Description("Step you're not supposed to see"), allure.Action(func() {}))
}))
}
14 changes: 6 additions & 8 deletions example/example_functions.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package example

import "github.com/dailymotion/allure-go"
import (
"github.com/dailymotion/allure-go"
)

func doSomething() {
allure.Step("Something", func() {
allure.Step(allure.Description("Something"), allure.Action(func() {
doSomethingNested()
})
}))
}

func doSomethingNested() {
allure.Step("Because we can!", func() {})
}

func addSomeParameters(parameters map[string]interface{}) {
allure.StepWithParameter("Step with parameters", parameters, func() {})
allure.Step(allure.Description("Because we can!"), allure.Action(func() {}))
}
19 changes: 19 additions & 0 deletions example/example_new_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package example

import (
"github.com/dailymotion/allure-go"
"testing"
)

func TestNewTest(t *testing.T) {
allure.Test(
t,
allure.Description("New Test Description"),
allure.Action(func() {
allure.Step(
allure.Description("Step description"),
allure.Action(func() {

}))
}))
}
7 changes: 4 additions & 3 deletions example/example_panic_handling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

func TestPanicBasic(t *testing.T) {
allure.Test(t, "Panic handling test", func() {
panic("throwing a panic")
})
allure.Test(t, allure.Description("Panic handling test"),
allure.Action(func() {
panic("throwing a panic")
}))
}
14 changes: 9 additions & 5 deletions example/example_panic_in_step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import (
)

func TestPanicInStep(t *testing.T) {
allure.Test(t, "Panic handling test", func() {
allure.Step("step that will panic", func() {
panic("throwing a panic")
})
})
allure.Test(t,
allure.Description("Panic handling test"),
allure.Action(func() {
allure.Step(
allure.Description("step that will panic"),
allure.Action(func() {
panic("throwing a panic")
}))
}))
}
108 changes: 51 additions & 57 deletions example/example_parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,75 +27,69 @@ var parameters = map[string]interface{}{
func TestAllureParameterized(t *testing.T) {
for i := 0; i < 5; i++ {
t.Run("", func(t *testing.T) {
allure.TestWithParameters(t,
"Test with parameters",
map[string]interface{}{
"counter": i,
},
allure.TestLabels{},
func() {

})
allure.Test(t,
allure.Description("Test with parameters"),
allure.Parameter("counter", i),
allure.Action(func() {}))
})
}
}

func TestAllureParametersExample(t *testing.T) {
allure.TestWithParameters(t,
"This is a test to show allure implementation with a passing test",
parameters,
allure.TestLabels{},
func() {
allure.Step(fmt.Sprintf("Number: %d", parameters["int"]), func() {})
allure.Step(fmt.Sprintf("String: %s", parameters["string"]), func() {})
allure.Step(fmt.Sprintf("Interface: %+v", parameters["struct"]), func() {})
})
allure.Test(t,
allure.Parameters(parameters),
allure.Description("This is a test to show allure implementation with a passing test"),
allure.Action(func() {
allure.Step(allure.Description(fmt.Sprintf("Number: %d", parameters["int"])), allure.Action(func() {}))
allure.Step(allure.Description(fmt.Sprintf("String: %s", parameters["string"])), allure.Action(func() {}))
allure.Step(allure.Description(fmt.Sprintf("Interface: %+v", parameters["struct"])), allure.Action(func() {}))
}))
}

func TestAllureStepWithParameters(t *testing.T) {
allure.Test(t, "Test with steps that have parameters", func() {
for i := 0; i < 5; i++ {
allure.StepWithParameter("Step with parameters", map[string]interface{}{"counter": i}, func() {})
}
allure.SkipStepWithParameter("Step with parameters", "Skip this step with parameters", map[string]interface{}{"counter": 6}, func() {})
})
allure.Test(t,
allure.Description("Test with steps that have parameters"),
allure.Action(func() {
for i := 0; i < 5; i++ {
allure.Step(
allure.Description("Step with parameters"),
allure.Parameter("counter", i),
allure.Action(func() {}))
}
allure.SkipStep(allure.Description("Step with parameters"), allure.Reason("Skip this step with parameters"), allure.Parameter("counter", 5), allure.Action(func() {}))
}))
}

func TestAllureParameterTypes(t *testing.T) {
allure.TestWithParameters(t,
"Test parameter types",
map[string]interface{}{
"uintptr": uintptr(10),
"uint": uint(10),
"uint8": uint8(10),
"uint16": uint16(10),
"uint32": uint32(10),
"uint64": uint64(10),
"int": int(10),
"int8": int8(10),
"int16": int16(10),
"int32": int32(10),
"int64": int64(10),
"float32": float32(10.5),
"float64": float64(10.5),
"complex64": complex(float32(2), float32(-2)),
"complex128": complex(float64(2), float64(-2)),
},
allure.TestLabels{},
func() {})
allure.Test(t,
allure.Description("Test parameter types"),
allure.Parameter("uintptr", uintptr(10)),
allure.Parameter("uint", uint(10)),
allure.Parameter("uint8", uint8(10)),
allure.Parameter("uint16", uint16(10)),
allure.Parameter("uint32", uint32(10)),
allure.Parameter("uint64", uint64(10)),
allure.Parameter("int", int(10)),
allure.Parameter("int8", int8(10)),
allure.Parameter("int16", int16(10)),
allure.Parameter("int32", int32(10)),
allure.Parameter("int64", int64(10)),
allure.Parameter("float32", float32(10.5)),
allure.Parameter("float64", float64(10.5)),
allure.Parameter("complex64", complex(float32(2), float32(-2))),
allure.Parameter("complex128", complex(float64(2), float64(-2))),
allure.Action(func() {}))
}

func TestAllureWithLabels(t *testing.T) {
allure.TestWithParameters(t, "Test with labels",
nil,
allure.TestLabels{
Epic: "Epic Epic of Epicness",
Lead: "Duke Nukem",
Owner: "Rex Powercolt",
Severity: severity.Critical,
Story: []string{"story1", "story2"},
Feature: []string{"feature1", "feature2"},
}, func() {

})
allure.Test(t, allure.Description("Test with labels"),
allure.Epic("Epic Epic of Epicness"),
allure.Lead("Duke Nukem"),
allure.Owner("Rex Powercolt"),
allure.Severity(severity.Critical),
allure.Story("story1"),
allure.Story("story2"),
allure.Feature("feature1"),
allure.Feature("feature2"),
allure.Action(func() {}))
}
14 changes: 14 additions & 0 deletions example/example_skip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package example

import (
"github.com/dailymotion/allure-go"
"testing"
)

func TestSkip(t *testing.T) {
allure.Test(t,
allure.Description("Skip test"),
allure.Action(func() {
allure.SkipStep(allure.Description("Skip"), allure.Action(func() {}), allure.Reason("reason"))
}))
}
Loading

0 comments on commit b778d93

Please sign in to comment.