Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to set allureID for testcase before run the 'BeforeAll' method in suite #88

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The project started as a fork of testify, but over time it got its own runner an
+ [XSkip](#xskip)
+ [:rocket: Parametrized tests](#parametrized-test)
+ [Setup test](#setup-test)
+ [Add ALLURE_ID to the tests before executing BeforeAll function](#Add-ALLURE_ID-to-the-tests-before-executing-BeforeAll-function)

## :zap: Features

Expand Down Expand Up @@ -728,10 +729,10 @@ type ParametrizedSuite struct {
ParamCities []string
}

func (s *ParametrizedSuite) BeforeAll(t provider.T) {
func (s *ParametrizedSuite) InitTestParams() {
for i := 0; i < 10; i++ {
s.ParamCities = append(s.ParamCities, fake.City())
}
}
}

func (s *ParametrizedSuite) TableTestCities(t provider.T, city string) {
Expand Down Expand Up @@ -805,3 +806,39 @@ func TestRunner(t *testing.T) {
Allure output:

![](.resources/example_setup_test.png)

### [Add ALLURE_ID to the tests before executing BeforeAll function](examples/suite_demo/allureid_test.go)

Function `AddAllureIDMapping(testName, allureID string)` allows to set ALLURE_ID label to the test during the suit's tests collecting step.
If the code into `BeforeAll` function fails, Allure Report will not duplicate testcases in TestOps.

Test code:

```go
package suite_demo

import (
"testing"

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

type AllureIdSuite struct {
suite.Suite
}

func (s *AllureIdSuite) BeforeAll(t provider.T) {
// code that can fail here
}

func (s *AllureIdSuite) TestMyTestWithAllureID(t provider.T) {
// code of your test here
}

func TestNewDemo(t *testing.T) {
ais := new(AllureIdSuite)
ais.AddAllureIDMapping("TestMyTestWithAllureID", "12345")
suite.RunSuite(t, ais)
}
```
26 changes: 26 additions & 0 deletions examples/suite_demo/allureid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package suite_demo

import (
"testing"

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

type AllureIdSuite struct {
suite.Suite
}

func (s *AllureIdSuite) BeforeAll(t provider.T) {
// code that can fail here
}

func (s *AllureIdSuite) TestMyTestWithAllureID(t provider.T) {
// code of your test here
}

func TestNewDemo(t *testing.T) {
ais := new(AllureIdSuite)
ais.AddAllureIDMapping("TestMyTestWithAllureID", "12345")
suite.RunSuite(t, ais)
}
8 changes: 8 additions & 0 deletions pkg/framework/runner/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ type AllureAfterSuite interface {
AfterAll(t provider.T)
}

// WithTestPramsSuite has an InitTestParams method, which will run before
// collecting the tests in the suite.
type WithTestPramsSuite interface {
InitTestParams()
}

type TestSuite interface {
GetRunner() TestRunner
SetRunner(runner TestRunner)
AddAllureIDMapping(testName, allureID string)
FindAllureID(testName string) (id string, ok bool)
}

type TestingT interface {
Expand Down
71 changes: 41 additions & 30 deletions pkg/framework/runner/suite_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,46 @@ func newSuiteRunner(realT TestingT, packageName, suiteName, parentSuite string,
suite: suite,
}
r = collectTests(r, suite)
r = collectParametrizedTests(r, suite)
r = collectHooks(r, suite)

return r
}

// collectParametrizedTests executes InitTestParams function, finds test methods with tableTestPrefix,
// gets map with parameters, gets map with parameterized tests,
// replaces tests in runner with parameterized tests with results
func collectParametrizedTests(runner *suiteRunner, suite TestSuite) *suiteRunner {
if initTestParamsSuit, ok := suite.(WithTestPramsSuite); ok {
initTestParamsSuit.InitTestParams()
}
newTests := make(map[string]Test)
for k, v := range runner.tests {
newTests[k] = v
}
for name, test := range runner.tests {
if strings.HasPrefix(name, tableTestPrefix) {
params, err := getParams(runner.suite, name)
if err != nil {
panic(err)
}
temp := getParamTests(test, params)
delete(newTests, name)
for tName, body := range temp {
tResult := body.GetMeta().GetResult()
id, ok := suite.FindAllureID(tName)
if ok {
tResult.AddLabel(allure.IDAllureLabel(id))
}
newTests[tName] = body
runner.internalT.GetProvider().GetSuiteMeta().GetContainer().AddChild(tResult.UUID)
}
}
}
runner.tests = newTests
return runner
}

// collectTests filters suite methods according to set regular expression and
// adds filtered methods to tests of runner
func collectTests(runner *suiteRunner, suite TestSuite) *suiteRunner {
Expand All @@ -81,7 +116,7 @@ func collectTests(runner *suiteRunner, suite TestSuite) *suiteRunner {
method := methodFinder.Method(i)
ok, err := methodFilter(method.Name)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "allire-go: invalid regexp for -m: %s\n", err)
_, _ = fmt.Fprintf(os.Stderr, "allure-go: invalid regexp for -m: %s\n", err)
os.Exit(1)
}

Expand All @@ -90,6 +125,10 @@ func collectTests(runner *suiteRunner, suite TestSuite) *suiteRunner {
}

testMeta := adapter.NewTestMeta(suiteFullName, suiteName, method.Name, packageName)
id, ok := suite.FindAllureID(method.Name)
if ok {
testMeta.GetResult().AddLabel(allure.IDAllureLabel(id))
}
runner.tests[method.Name] = &testMethod{
testMeta: testMeta,
testBody: method,
Expand All @@ -107,34 +146,6 @@ type parametrizedTest interface {
GetMeta() provider.TestMeta
}

// parametrizedWrap executes beforeAll function, finds test methods with tableTestPrefix,
// gets map with parameters, gets map with parameterized tests,
// replaces tests in runner with parameterized tests with results
func parametrizedWrap(runner *suiteRunner, beforeAll func(provider.T)) func(t provider.T) {
return func(t provider.T) {
beforeAll(t)
newTests := make(map[string]Test)
for k, v := range runner.tests {
newTests[k] = v
}
for name, test := range runner.tests {
if strings.HasPrefix(name, tableTestPrefix) {
params, err := getParams(runner.suite, name)
if err != nil {
panic(err)
}
temp := getParamTests(test, params)
delete(newTests, name)
for tName, body := range temp {
newTests[tName] = body
runner.internalT.GetProvider().GetSuiteMeta().GetContainer().AddChild(body.GetMeta().GetResult().UUID)
}
}
}
runner.tests = newTests
}
}

// getParamTests create instance of TestAdapter for every param from params
// and returns map whose elements are a pair (<param name>, <pointer to instance of testMethod>)
func getParamTests(parentTest Test, params map[string]interface{}) map[string]Test {
Expand Down Expand Up @@ -204,7 +215,7 @@ func getParams(suite TestSuite, methodName string) (res map[string]interface{},

func collectHooks(runner *suiteRunner, suite TestSuite) *suiteRunner {
if beforeAll, ok := suite.(AllureBeforeSuite); ok {
runner.BeforeAll(parametrizedWrap(runner, beforeAll.BeforeAll))
runner.BeforeAll(beforeAll.BeforeAll)
}

if beforeEach, ok := suite.(AllureBeforeTest); ok {
Expand Down
15 changes: 14 additions & 1 deletion pkg/framework/suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@ import (
)

type Suite struct {
runner runner.TestRunner
runner runner.TestRunner
allureIDMapping map[string]string
}

func (s *Suite) AddAllureIDMapping(testName, allureID string) {
if s.allureIDMapping == nil {
s.allureIDMapping = make(map[string]string)
}
s.allureIDMapping[testName] = allureID
}

func (s *Suite) FindAllureID(testName string) (id string, ok bool) {
id, ok = s.allureIDMapping[testName]
return
}

func (s *Suite) GetRunner() runner.TestRunner {
Expand Down
Loading