testify icon indicating copy to clipboard operation
testify copied to clipboard

Provision for Skipping a test

Open nanjekyejoannah-zz opened this issue 7 years ago • 10 comments

We have many reasons why we need to skip particular tests. I needed to provide for skipping a test given some condition depending on the environment the tests are running in. Apparently this is not available.

nanjekyejoannah-zz avatar Jul 10 '17 14:07 nanjekyejoannah-zz

Sounds like you need to call testing.T.Skip() based on your conditions (flags, env vars, ...): https://godoc.org/testing#T.Skip

bits01 avatar Jul 10 '17 19:07 bits01

You see am using testify/suite for the tests am writing and my tests look like this. I decided to use suite in order to manage my teardowns and setup in a saner way.

func (suite *S3Suite) TestObjectCreateBadContentlengthNone() {

// some test code
}

If am to use testing.T.Skip. This will become

func TestObjectCreateBadContentlengthNone() {

	if sommecondition {

		testing.t.Skip("message for skip")
	}

	// more testcode
}

As you can see I have to remove the test I want to skip from the suite. I tried to use testing.T.Skip while keeping the test in the suite and it did not work. This also means that I have to provide separate teardown methods for this test that is outside the suite which did not play nice with me. Of course for now I am using testing.T.Skip for all tests I may want to skip for lack of a good option.

Therefore if testify/suite had a provision of skipping, I would have some kind of consistency. I will still have my test in question part of the suite i created and the test will still use the teardown/setup methods like the other tests in the suite. Plus Teardown/setup without using testify/suite is not so clean IMO.

nanjekyejoannah-zz avatar Jul 11 '17 10:07 nanjekyejoannah-zz

Have you looked at using standard lib subtests instead? https://blog.golang.org/subtests

bits01 avatar Jul 11 '17 15:07 bits01

Well the idea of sub tests is a good one however it does not solve my problem.

There is no need for anyone running the test suite to decide what tests to execute. I want the tests themselves to skip some tests depending on the environment in which they are running. The person running the tests should just do go test -v . For example in my case I want some tests to skip when there is no SSL connection.In my test project, the tests that depend on this condition are so many and spread in different files. The subtest way may not be a very good solution.

nanjekyejoannah-zz avatar Jul 11 '17 15:07 nanjekyejoannah-zz

Subtests can skip based on their own conditions. They are just normal tests, you just group them together so you can do the pre setup and post teardown, not any different from suites.

bits01 avatar Jul 11 '17 15:07 bits01

This person had the same thing in mind while reporting this issue. check out that thread.

nanjekyejoannah-zz avatar Jul 22 '17 10:07 nanjekyejoannah-zz

was looking for the same thing. So suite.T().Skip() will skip only the current method-test right?

Mistobaan avatar Feb 03 '18 00:02 Mistobaan

I found this issue as I was about to suggest that testify support assume in addition to assert and require. Coming from the Java world, the two major assertion libraries I'm familiar with have somewhat parallel assumptions.

The idea behind assumptions is that in some cases, you want to conditionally skip a test (instead of outright failing it) as described in this issue. Since the require and forward methods are already generated from the assertions, it seems like it would be practical to add generated assumptions to this library.

smoyer64 avatar Jul 02 '19 19:07 smoyer64

@Mistobaan: yes. suite.T().Skip() will skip only the current test method. (workaround until the feature is implemented)

charleswhchan avatar Aug 18 '19 23:08 charleswhchan

How to skip the whole suite? I am finding a way to skip some test suites in the CI env.

mgsweet avatar Apr 11 '22 08:04 mgsweet