Skip to content

Commit

Permalink
Merge pull request ozontech#31 from ozontech/release/issue-30
Browse files Browse the repository at this point in the history
Release/issue 30
  • Loading branch information
koodeex committed Aug 23, 2022
2 parents 4be753f + d698557 commit 9a47ceb
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 14 deletions.
Binary file added .resources/example_setup_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,27 @@ The project started as a fork of testify, but over time it got its own runner an
+ [Setup hooks](#setup-hooks)
+ [XSkip](#xskip)
+ [:rocket: Parametrized tests](#parametrized-test)
+ [Setup test](#setup-test)

## :zap: Features

Providing a separate package allows you to customize your work with allure.<br>

### What's new?

**Release v0.6.17**

#### WithTestSetup/WithTestTeardown methods

Now you can improve your reports with `t.WithTestSetup(func (provider.T))` or `t.WithTeardown(func (provider.T))`.<br>
If you need some more setup customization, now you can pass your steps to `Set up` and `Tear down` right from the test!<br>

Check out [our example](./examples/suite_demo/setup_test.go) for more information, or check out example below:

:information_source: Feature works __only__ with Test. If you try to run it in Before/After hook or inside itself - nothing will happen.<br>
:information_source: Yes, it works with t.Parallel, but HIGHLY recommended to run t.Parallel AFTER TestSetup.<br>
:information_source: Yes, it works with TableTest.<br>

**Release v0.6.16**

#### :zap: Parametrized tests
Expand Down Expand Up @@ -731,3 +745,61 @@ func TestNewParametrizedDemo(t *testing.T) {
Output to Allure:

![](.resources/example_table_test.png)

### [Setup test](examples/suite_demo/setup_test.go)

This feature allows you to extend your test setting up/tearing down functional

Test code:

```go
package suite_demo

import (
"context"
"testing"

"github.com/ozontech/allure-go/pkg/framework/provider"
"github.com/ozontech/allure-go/pkg/framework/suite"
)

type SetupSuite struct {
suite.Suite
}

func (s *SetupSuite) TestMyTest(t provider.T) {
var (
v1 string
v2 int
ctx context.Context
)
t.WithTestSetup(func(t provider.T) {
t.WithNewStep("init v1", func(sCtx provider.StepCtx) {
v1 = "string"
sCtx.WithNewParameters("v1", v1)
})
t.WithNewStep("init v2", func(sCtx provider.StepCtx) {
v2 = 123
sCtx.WithNewParameters("v2", v2)
})
t.WithNewStep("init ctx", func(sCtx provider.StepCtx) {
ctx = context.Background()
sCtx.WithNewParameters("ctx", ctx)
})
})
defer t.WithTestTeardown(func(t provider.T) {
t.WithNewStep("Close ctx", func(sCtx provider.StepCtx) {
ctx.Done()
sCtx.WithNewParameters("ctx", ctx)
})
})
}

func TestRunner(t *testing.T) {
suite.RunSuite(t, new(SetupSuite))
}
```

Allure output:

![](.resources/example_setup_test.png)
3 changes: 3 additions & 0 deletions examples/suite_demo/new_parametrized_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build examples_new
// +build examples_new

package suite_demo

import (
Expand Down
111 changes: 111 additions & 0 deletions examples/suite_demo/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//go:build examples_new
// +build examples_new

package suite_demo

import (
"context"
"fmt"
"testing"

"github.com/jackc/fake"
"github.com/ozontech/allure-go/pkg/allure"
"github.com/ozontech/allure-go/pkg/framework/provider"
"github.com/ozontech/allure-go/pkg/framework/suite"
)

type Example struct {
country string
number int
}

func (e *Example) String() string {
return fmt.Sprintf("%s#%d", e.country, e.number)
}

type SetupSuite struct {
suite.Suite
ParamMyTest []*Example
}

func (s *SetupSuite) BeforeAll(t provider.T) {
for i := 0; i < 10; i++ {
s.ParamMyTest = append(s.ParamMyTest, &Example{
country: fake.Country(),
number: fake.Year(1900, 2000),
})
}
}

func (s *SetupSuite) BeforeEach(t provider.T) {
t.Epic("Demo")
t.Feature("BeforeAfter")
t.Step(allure.NewSimpleStep("This Step will be before Each"))
}

func (s *SetupSuite) TableTestMyTest(t provider.T, example *Example) {
t.Titlef("TableTest With Setup - %s", example)
t.Descriptionf(`
Test will unpack all data from passed parameter to the variables in WithTestSetup func.
After test finish, it will do ctx.Done() in TestTearDown.
All Setup and TearDown tests will be add as Befores and Afters to test's container.
Used Data: %s`, example)
t.Tags("Parametrized", "Parallel", "Setup", "BeforeAfter")

t.Parallel()
var (
country string
year int
ctx context.Context
)
t.WithTestSetup(func(t provider.T) {
t.WithNewStep("init country", func(sCtx provider.StepCtx) {
country = example.country
sCtx.WithNewParameters("country", country)
})
t.WithNewStep("init year", func(sCtx provider.StepCtx) {
year = example.number
sCtx.WithNewParameters("year", year)
})
t.WithNewStep("init ctx", func(sCtx provider.StepCtx) {
ctx = context.Background()
sCtx.WithNewParameters("ctx", ctx)
})
})

t.Require().NotEqual("PonyCountry", country, "No magic countries in the list")
t.Require().NotEqual(2007, year, "No one returned to 2007")
t.Require().NotNil(ctx, "Not empty context")

defer t.WithTestTeardown(func(t provider.T) {
t.WithNewStep("Close ctx", func(sCtx provider.StepCtx) {
ctx.Done()
sCtx.WithNewParameters("ctx", ctx)
})
})
}

func (s *SetupSuite) TestMyOtherTest(t provider.T) {
t.Title("Just Test WithSetup")
t.Description(`
Test will prepare some data at TestSetup.`)
t.Tags("Parallel", "Setup", "BeforeAfter")

t.Parallel()
var (
name string
age int
)

t.WithTestSetup(func(t provider.T) {
t.WithNewStep("init args", func(sCtx provider.StepCtx) {
name = fake.FullName()
age = fake.Day()
sCtx.WithNewParameters("name", name, "age", age)
})
})
}

func TestRunner(t *testing.T) {
suite.RunSuite(t, new(SetupSuite))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ replace (
require (
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733
github.com/ozontech/allure-go/pkg/allure v0.6.3
github.com/ozontech/allure-go/pkg/framework v0.6.16
github.com/ozontech/allure-go/pkg/framework v0.6.17
)

require (
Expand Down
10 changes: 6 additions & 4 deletions pkg/framework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ during test runtime any other way (`SystemLabels` interface) but ReplaceLabel me

##### Behaviour manipulation methods (`T` interface)

| Method | Description |
|:----------------|:------------------------------------------------------------------------------------------------------------------------------------------:|
| `XSkip()` | Marks test as expected to fail. If test going to fail with assert it will be marked skip instead. |
| `SkipOnPrint()` | Marks report as skip on print. That means that report won't be created for current test. Use it for clean reports from parent of subtests. |
| Method | Description |
|:----------------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------:|
| `XSkip()` | Marks test as expected to fail. If test going to fail with assert it will be marked skip instead. |
| `SkipOnPrint()` | Marks report as skip on print. That means that report won't be created for current test. Use it for clean reports from parent of subtests. |
| `WithTestSetup(func (t provider.T))` | Switches context of the test for before each and run passed func with BeforeEach context (all steps will to Set up allure section) |
| `WithTestTeardown(func (t provider.T))` | Switches context of the test for after each and run passed func with AfterEach context (all steps will to Tear down allure section) |

### provider.StepCtx

Expand Down
28 changes: 22 additions & 6 deletions pkg/framework/core/allure_manager/manager/description.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
package manager

import (
"fmt"

"github.com/ozontech/allure-go/pkg/allure"
)

// Title changes default test name to title(string)
func (a *allureManager) Title(title string) {
// Title changes default test name to title(using fmt.Sprint)
func (a *allureManager) Title(args ...interface{}) {
a.safely(func(result *allure.Result) {
result.Name = fmt.Sprint(args...)
})
}

// Titlef changes default test name to title(using fmt.Sprintf)
func (a *allureManager) Titlef(format string, args ...interface{}) {
a.safely(func(result *allure.Result) {
result.Name = fmt.Sprintf(format, args...)
})
}

// Description provides description to test result(using fmt.Sprint)
func (a *allureManager) Description(args ...interface{}) {
a.safely(func(result *allure.Result) {
result.Name = title
result.Description = fmt.Sprint(args...)
})
}

// Description provides description to test result
func (a *allureManager) Description(description string) {
// Descriptionf provides description to test result(using fmt.Sprintf)
func (a *allureManager) Descriptionf(format string, args ...interface{}) {
a.safely(func(result *allure.Result) {
result.Description = description
result.Description = fmt.Sprintf(format, args...)
})
}
43 changes: 43 additions & 0 deletions pkg/framework/core/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import (
"github.com/ozontech/allure-go/pkg/framework/asserts_wrapper/helper"
"github.com/ozontech/allure-go/pkg/framework/core/allure_manager/manager"
"github.com/ozontech/allure-go/pkg/framework/core/allure_manager/testplan"
"github.com/ozontech/allure-go/pkg/framework/core/constants"
"github.com/ozontech/allure-go/pkg/framework/provider"
)

const logMsgSetupTearDown = "%s will be skipped. Reason: wrong context. Expected: %s; Actual: %s"

type Common struct {
provider.TestingT
provider.Provider
Expand Down Expand Up @@ -188,6 +191,46 @@ func (c *Common) Skipf(format string, args ...interface{}) {
c.TestingT.Skipf(format, args...)
}

// WithTestSetup ...
func (c *Common) WithTestSetup(setup func(provider.T)) {
currentContext := c.GetProvider().ExecutionContext().GetName()
if currentContext != constants.TestContextName {
c.Logf(logMsgSetupTearDown, "WithTestSetup", constants.TestContextName, currentContext)
return
}
c.subContextWrapper(currentContext, BeforeEach, setup)
}

// WithTestTeardown ...
func (c *Common) WithTestTeardown(teardown func(provider.T)) {
currentContext := c.GetProvider().ExecutionContext().GetName()
if currentContext != constants.TestContextName {
c.Logf(logMsgSetupTearDown, "WithTestTeardown", constants.TestContextName, currentContext)
return
}
c.subContextWrapper(currentContext, AfterEach, teardown)
}

func (c *Common) subContextWrapper(currentContext string, newContext HookType, hook func(t provider.T)) {
defer func() {
rec := recover()
c.GetProvider().TestContext()
if rec != nil {
errMsg := fmt.Sprintf("%s panicked: %v\n%s", currentContext, rec, debug.Stack())
TestError(c, c.GetProvider(), currentContext, errMsg)
}
}()
switch newContext {
case BeforeEach:
c.GetProvider().BeforeEachContext()
case AfterEach:
c.GetProvider().AfterEachContext()
default:
panic(fmt.Sprintf("Wrong execution context used to run func. Expected: %s OR %s; Actual: %s", BeforeEach, AfterEach, newContext))
}
hook(c)
}

// Run runs test body as test with passed tags
func (c *Common) Run(testName string, testBody func(provider.T), tags ...string) (res *allure.Result) {
parentCallers := strings.Split(c.RealT().Name(), "/")
Expand Down
6 changes: 4 additions & 2 deletions pkg/framework/provider/allure.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ type Links interface {
}

type DescriptionFields interface {
Title(title string)
Description(description string)
Title(args ...interface{})
Titlef(format string, args ...interface{})
Description(args ...interface{})
Descriptionf(format string, args ...interface{})
}

type AllureSteps interface {
Expand Down
2 changes: 2 additions & 0 deletions pkg/framework/provider/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type T interface {
LogfStep(format string, args ...interface{})
WithNewStep(stepName string, step func(sCtx StepCtx), params ...allure.Parameter)
WithNewAsyncStep(stepName string, step func(sCtx StepCtx), params ...allure.Parameter)
WithTestSetup(setup func(T))
WithTestTeardown(teardown func(T))
}

type StepCtx interface {
Expand Down
1 change: 0 additions & 1 deletion pkg/framework/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ func (r *runner) RunTests() SuiteResult {
r.realT().Run(test.GetMeta().GetResult().Name, func(t *testing.T) {
defer wg.Done()
defer func() {
t.Logf(test.GetMeta().GetResult().UUID.String())
result.NewResult(finishTest(t, test.GetMeta()))
}()
testT := setupTest(t, r.t().GetProvider(), test.GetMeta())
Expand Down

0 comments on commit 9a47ceb

Please sign in to comment.