Spock - Do not run Specs simultaneously - gradle

I'm running functional tests using Spock where the tests manipulate the database, and the Specs can conflict with each other if run simultaneously. From my web debugger, it appears that when I run gradle test that the calls to the web service from different Specs are being called simultaneously. The Specs themselves are being called with the #Stepwise annotation, and the tests are run in the correct order. How can I have Specs run separately in some order? They do not need to run in any specific order.

Related

How to create spring boot test suite

Say I have 10 spring boot test class (annotated with #RunWith(SpringRunner.class) and #SpringBootTest)
Each test needs to launch spring container for like 10 seconds, although the container might do the same init.
So I may need 100 seconds for "mvn test".
Is there a way I can group my 10 test class into 1 suite, and let the container only start once.
So I can:
Only run the suite for "mvn test". (with proper naming for individual test class)
Optionally run individual test in IDE.
Spring uses Cache Management to cache the Application Context between tests:
By default, once loaded, the configured ApplicationContext is reused for each test. Thus, the setup cost is incurred only once per test suite, and subsequent test execution is much faster. In this context, the term “test suite” means all tests run in the same JVM — for example, all tests run from an Ant, Maven, or Gradle build for a given project or module. In the unlikely case that a test corrupts the application context and requires reloading (for example, by modifying a bean definition or the state of an application object) the TestContext framework can be configured to reload the configuration and rebuild the application context before executing the next test. (https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/testing.html#integration-testing)
So this mechanism tries to execute your integration tests on an already running Application Context if possible. As you see multiple Application Context launches, this indicates your tests somehow use a different setup e.g. different profiles active, test properties, MockBeans etc.
The Spring documentation provides an overview on which indicators it puts an Application Context in its cache: https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/testing.html#testcontext-ctx-management-caching
If you e.g. don't change any test property for your integration tests, Spring can run all of them on only one Application Context and be extremely efficient.
Another indicator for your current behaviour might be the use of #DirtiesContext which leads to a fresh Application Context after your test executes.

How to order unit test execution from Gradle?

Is there any way to specify the order of the unit tests classes run by a Gradle Test task?
I'd like to get some known longer-running tests at either the front or the back of the list, but don't know if it's possible without splitting my tests' execution between multiple tasks.
Running JUnit 4.12 and Gradle 4.5.
Gradle simply delegates execution to the JUnit runner.
So if you want specific test class ordering, you will need to create a Suite and specify the test classes in the order you want, see the JUnit documentation for this.
Now, given the flexibility of Gradle in terms of having different source roots, I would strongly recommend doing the separation at the Gradle level, by create extra test source roots, test task and the like. This will allow you to effectively control when these long running tests are run in a standard build execution, but also to skip them or run these only when desired. The ordering at the JUnit level will not give that flexibility without much more tweaking. See the Gradle documentation on adding source sets.

Control the running order of the Spock specs

I'm trying to run Spock tests in a specific order, I'm using gradle to run the tests.
I want to be able to control the test order.
I've found I can use the suit, but it doesn't scale for my tests.
is there any other way to run spock tests in a specific order?

Multiple Profiles For Spring Integration Tests?

I need different profiles for a few things. First we have the issue of my databases. When I run local tests I expect to use one datasource. When I run an acceptance profile (for a CI acceptance build), I expect to use a different datasource. Finally, when I run acceptance not in test I expect to use a third datasource. How I imagined this would work is.
/src/main/resources/application.properties
/src/main/resources/application-acceptance.properties
/src/test/resources/application-test.properties
/src/test/resources/application-acceptance-test.properties
However when I run mvn clean install -Dspring.profiles.active=acceptance it does not run the application-accepatnce-test.properties.
Finally, I would like to be able to run a mvn install while running the tests but not the integrations test. For this I imagine I would add a -Dspring.profiles.active=nointegration and then simply add an #ActiveProfiles('!nointegration') on the integration tests.
I've had no luck with either of these. Is it even possible to get profiles on test runs?
If it helps I am using Spring Boot 1.3.0.RELEASE.
EDIT:
On my integration tests I have #ActiveProfiles("test"). Is there any way to generate the profile here based on the java-opt spring.profiles.active?

#BeforeMethod(TestNG) and #BeforeTest(TestNG) counterpart while writing test cases in Jasmine

I am writing test cases using Protractor and Jasmine framework.
But as I have background of writing test cases in Java using TestNG framework, my mind keep on forcing me to use something like #BeforeMethod and #BeforeTest of TestNG framework in Jasmine, along with Groups property of TestNG.
Are there any counterparts for the same in Jasmine framework?
I have used afterEach and beforeEach method but it applies for every it() function of every describe, there is no method to apply running on the basis of some Groups.
Thanks,
Mohit
In a spec file
beforeAll will run before ALL of the specs (before beforeEach)
beforeEach will run before each it
afterEach will run after each it
afterAll will run after ALL of the specs (after afterEach)
In protractor.conf.js
beforeLaunch will run right before the browser is available
onPrepare will run right after the browser is available (before the specs)
onCleanUp will run right after the last spec finishes

Resources