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

Parametrized tests v2 #98

Merged
merged 8 commits into from
Jul 3, 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
102 changes: 89 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +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)
+ [Prevent loosing allureID in test results](#Prevent-loosing-allureID-in-test-results)

## :zap: Features

Expand Down Expand Up @@ -729,10 +729,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 @@ -807,12 +807,13 @@ Allure output:

![](.resources/example_setup_test.png)

### [Add ALLURE_ID to the tests before executing BeforeAll function](examples/suite_demo/allureid_test.go)
### Prevent loosing allureID in test results

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.
When suit fails at the setup stage (beforeAll), report will not contain `ALLURE_ID` field.
To prevent it you can use `GetAllureID(testName string) string` method for common tests and
`InitializeTestsParams()` method for parametrized tests.

Test code:
**Example for `GetAllureID` method:**

```go
package suite_demo
Expand All @@ -824,21 +825,96 @@ import (
"github.com/ozontech/allure-go/pkg/framework/suite"
)

type AllureIdSuite struct {
type AllureIDSuite struct {
suite.Suite
}

func (s *AllureIdSuite) BeforeAll(t provider.T) {
func (testSuit *AllureIDSuite) GetAllureID(testName string) string {
switch testName {
case "TestWithAllureIDFirst":
return "9001"
case "TestWithAllureIDSecond":
return "9002"
default:
return ""
}
}

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

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

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

func TestNewDemo(t *testing.T) {
ais := new(AllureIdSuite)
ais.AddAllureIDMapping("TestMyTestWithAllureID", "12345")
suite.RunSuite(t, ais)
suite.RunSuite(t, new(AllureIDSuite))
}

```

**Example for `InitializeTestsParams` method:**

```go
package suite_demo

import (
"testing"

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

type CitiesParam struct {
allureID string
title string
value string
}

func (p CitiesParam) GetAllureID() string {
return p.allureID
}
func (p CitiesParam) GetAllureTitle() string {
return p.title
}

type ParametrizedSuite struct {
suite.Suite
ParamCities []CitiesParam
}

func (s *ParametrizedSuite) InitializeTestsParams() {
s.ParamCities = make([]CitiesParam, 2)
s.ParamCities[0] = CitiesParam{
title: "Title for city test #1",
allureID: "101",
value: fake.City(),
}

s.ParamCities[1] = CitiesParam{
title: "Title for city test #2",
allureID: "102",
value: fake.City(),
}
}


func (s *ParametrizedSuite) BeforeAll(t provider.T) {
// setup suit here
}

func (s *ParametrizedSuite) TableTestCities(t provider.T, city CitiesParam) {
t.Parallel()
t.Require().NotEmpty(city.value)
}

func TestNewParametrizedDemo(t *testing.T) {
suite.RunSuite(t, new(ParametrizedSuite))
}
```
26 changes: 0 additions & 26 deletions examples/suite_demo/allureid_test.go

This file was deleted.

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.13
github.com/ozontech/allure-go/pkg/framework v0.6.31
github.com/ozontech/allure-go/pkg/framework v0.6.32
)

require (
Expand Down
23 changes: 17 additions & 6 deletions pkg/framework/runner/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,28 @@ 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()
// AllureIDSuite has a GetAllureID method,
// which will produce allureIDs for the test by its name
type AllureIDSuite interface {
GetAllureID(testName string) string
}

// ParametrizedSuite suit can initialize parameters for
// parametrized test before running hooks
type ParametrizedSuite interface {
InitializeTestsParams()
}

// ParametrizedTestParam parameter for parametrized test
// with custom AllureId and Title
type ParametrizedTestParam interface {
GetAllureID() string
GetAllureTitle() string
}

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

type TestingT interface {
Expand Down
2 changes: 1 addition & 1 deletion pkg/framework/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func setupErrorHandler(msg string, err error, meta provider.TestMeta, result Sui
defer mtx.Unlock()

tRes := NewTestResult(meta.GetResult(), meta.GetContainer())
tRes.GetResult().Status = allure.Unknown
tRes.GetResult().Status = allure.Failed
tRes.GetResult().SetStatusMessage(msg)
tRes.GetResult().SetStatusTrace(fmt.Sprintf("%s. Reason:\n%s", msg, err.Error()))
_ = tRes.Print()
Expand Down
Loading
Loading