0

我想在每次测试完成后进行数据清理和执行后数据。根据我的文件夹结构,所有 *_test.go 文件都位于特定目录中。在项目级别,我创建了 customsuites/customsuite.go 文件。下面是我的文件夹结构。

.
|-- go.mod
|-- go.sum
|-- calculator
|   `-- calculator.go
|-- stringoperations
|   `-- string.go
|-- customsuites
|   `-- customtestsuite.go
`-- tests
    |-- calculator
    |   `-- calculator_test.go
    |-- string
    |   `-- string_test.go
    `-- testsuite_test.go

我在将 CustomTestSuite 结构导入测试时遇到问题,有没有办法将测试方法之前/之后分别保存在不同的包中?

customtestsuite.go

package customsuites

import (
    "fmt"
    "github.com/stretchr/testify/suite"
)

type CustomTestSuites struct {
    suite.Suite
}

func (suite *CustomTestSuites) BeforeTest(suiteName, testName string) {
    fmt.Println("Before Test" + suiteName + ":::" + testName)
}

func (suite *CustomTestSuites) AfterTest(suiteName, testName string) {
    fmt.Println("After Test" + suiteName + ":::" + testName)
}

testsuite_test.go

import (
    "github.com/priyankshah217/godog-poc/customsuites"
    "testing"
)

func (suite *customsuites.CustomTestSuites) Test_TestSuite(t *testing.T) {
    suite.Run(t, new(customsuites.CustomTestSuites))
}

计算器_test.go

package calculator

import (
    "github.com/priyankshah217/godog-poc/calculator"
    "github.com/priyankshah217/godog-poc/customsuites"
)

func (suite *customsuites.CustomTestSuites) TestMul() {
    type args struct {
        no1 int
        no2 int
    }
    tests := []struct {
        name string
        args args
        want int
    }{
        {"tc-1", struct {
            no1 int
            no2 int
        }{2, 3}, 6},
    }
    for _, tt := range tests {
        suite.Run(tt.name, func() {
            suite.Equal(tt.want, calculator.Mul(tt.args.no1, tt.args.no2))
        })
    }
}

func (suite *customsuites.CustomTestSuites) TestAdd() {
    type args struct {
        no1 int
        no2 int
    }
    tests := []struct {
        name string
        args args
        want int
    }{
        {
            "tc-1",
            struct {
                no1 int
                no2 int
            }{2, 3},
            5,
        },
    }
    for _, tt := range tests {
        suite.Run(tt.name, func() {
            suite.Equal(tt.want, calculator.Add(tt.args.no1, tt.args.no2))
        })
    }
}
4

0 回答 0