質問

I have hundreds of test specifications written in Spock. All of these are functional tests and can be run independently. But I have come across a situation where I need to run a specific test before running some other test.

This was very easy to achieve using Junit Test Suite and it was very straight forward in Eclipse. But since all my tests are groovy tests there is no easy way to create a Test Suite in Spring IDE for the spock tests (written in Groovy).

Can someone please share some ideas as to how we can create a Test suite and run some specific tests and also define the order of tests.

Any help would be much appreciated.

役に立ちましたか?

解決 3

Although I think that it won't allow you to specify the order of the tests, you could use Spock's Runner configuration or @IgnoreIf/@Require built-in extensions. Have a look at my response to a similar question. It's probably also worth having a look at RunnerConfiguration javadoc as it shows that you can include classes directly instead of using annotations.

他のヒント

Spock specifications are valid JUnit tests (or suites) as well. That's why they are recognized by tools such as STS. You should be able to add it to the test suites as well as other JUnit test.

On the other hand it doesn't sound as a good practise if your tests depend on execution order. If certain tasks needs to be performed before the test execution, it should be placed in setup() method. If that logic is common to more than one test, consider extracting it to the parent class.

If all you need is sequential execution of methods within a spec, have a look at @spock.lang.Stepwise, which is handy for testing workflows. Otherwise, you have the same possibilities as with plain JUnit: you can use JUnit (4) test suites, model test suites in your build tool of choice (which might not help within STS), or define test suites via Eclipse run configurations. I don't know how far support for the latter goes, but at the very least, it should allow you to run all tests in a package.

If the tests you want to run in a specific order are part of the same spock Specification, then you can use the @Stepwise annotation to direct that the tests (feature methods) are executed in the order they appear in the Specification class.

As others mentioned, its best to avoid this dependency if you can because of the complexity it introduces. For example, what happens if the first test fails? Does that leave the system in a undefined state for the subsequent tests? So it would be better to prevent the intra-test dependencies with setup() and cleanup() methods (or setupSpec() and cleanupSpec()).

Another option is to combine two dependent tests into a single multi-stage test with multiple when:/then: block pairs in sequence.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top