Skip to content

Commit

Permalink
fix broken parametrized tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dkx86 committed Jun 3, 2024
1 parent 4903fa0 commit 95db390
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 127 deletions.
41 changes: 2 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ 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 @@ -729,10 +728,10 @@ type ParametrizedSuite struct {
ParamCities []string
}

func (s *ParametrizedSuite) InitTestParams() {
func (s *ParametrizedSuite) BeforeAll(t provider.T) {
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 @@ -806,39 +805,3 @@ 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: 0 additions & 26 deletions examples/suite_demo/allureid_test.go

This file was deleted.

8 changes: 0 additions & 8 deletions pkg/framework/runner/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,9 @@ 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
69 changes: 29 additions & 40 deletions pkg/framework/runner/suite_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,46 +62,11 @@ 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 @@ -125,10 +90,6 @@ 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 @@ -146,6 +107,34 @@ 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 @@ -215,7 +204,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(beforeAll.BeforeAll)
runner.BeforeAll(parametrizedWrap(runner, beforeAll.BeforeAll))
}

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

type Suite struct {
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
runner runner.TestRunner
}

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

0 comments on commit 95db390

Please sign in to comment.