How to set order of classes with tests - junit5

I have several classes with integration tests and I need to set order, in which will be these classes proceed.
I use junit5 in version 5.4.1.
Please advice possible solutions.

You cannot control the order in which test classes are executed in JUnit Jupiter.
JUnit Jupiter only provides a mechanism for controlling the execution order of methods within a single test class.
See Test Execution Order in the User Guide for details.

Related

Spring boot test files execution ordering

I have a project with several hundred test files some of the test files use DataJpaTest annotation, some are MockMvc based controller tests and some uses mocked objects without database dependency, Based on test execution order I see context needs to be re-initialized for different flavors of test files, Is there a way to control execution of test files order so that context reload can be avoided? Say all mock tests first followed by controller tests and then DataJpaTest?
Right now test case execution taking about 30 minutes looking for way to improve the speed up test execution.
JUnit Jupiter provides options to control Test Execution Order.
However, you should look into your test setup and verify if your tests create too many Application Contexts.
Spring Test framework can cache Application Contexts and reuse them among different test suites. See Spring Test documentation for more info.

Execution order of test cases in Cucumber

Below is the structure how my Feature Files are divide. I have created Folders based on the functionalities and then added the scenarios inside them.
Now, I have to tag few test cases among them as Smoke Test cases and get them executed.
The point here is I need a specific order for that as in eg
Add Asset
Run Test
Schedule Test
Delete Asset
Since I will add something first and then work on it and delete it at the end
I know by default Cucumber executes test cases alphabetically but that would not solve my problem.
How can I achieve that?
I am using Java
Cucumber features/scenarios are run in Alphabetical order by feature file name.
However, if you specifically specify features, they should be run in the order as declared. For example:
#Cucumber.Options(features={"automatedTestingServices.feature", "smoketest.feature"})
You can achieve by setting priority or dependency, supported in QAF which is TestNg implementation for BDD. Setting priority with scenarion should do the needful for example:
with QAF scenario in DeleteAssets.feature may look like below:
#priority:100
#or you can set dependencies like below
##dependsOnGroups:['create','schedule']
#delete #otherGroup
Scenario: Delete existing Asset
Given ...
Note: gherkin syntax doesn't supports meta-data so you need to use either qaf bdd or bdd2 syntax and appropriate factory to run tests.
Yes, you can set a priority in cucumber scenarios. but not for the whole scenarios we can do that. inside methods we have declared in step definition file, can achieve that. Just put a keyword "Order" in the step definition file over the method based on the order of the method it will run as priority.
Click here for reference

Skipping test steps in testng

I am following POM approach with Testng for designing my framework . Consider a scenario wherein the test case got failed in the nth test step inside a #Test method.Can anyone suggest how can I skip the remaining test steps(from n+1 onwards)?
Since I am automating the manual test cases, each #Test belongs to 1 test case and so I cannot split up the steps into multiple #Test methods. When the test step is failed, it needs to skip the next steps in that #Test method and proceed to the next test case.
Also I need the count of test steps skipped in the result.
Kindly help.
Looks like you are basically looking for something around what a BDD tool such as Cucumber provides you. Cucumber lets you create a .feature file which contains one or more scenario (You can visualize each of your scenario to be one #Test annotated test method).
You could then leverage one of the Cucumber integrations i.e.,
Choose either JUnit (or)
Choose TestNG integration
and let one of these run your BDD tests.
Here when a particular step fails, then all subsequent steps would be aborted (which is what you are asking for)
Outside of Cucumber, I dont think you can have this done via any other mechanism. The reporting needs (such as how many steps were skipped etc.,) can be fulfilled by any cucumber based reports.
You should start from here : http://docs.cucumber.io/guides/10-minute-tutorial/

Testng : how to get all asserts within a test method?

I am trying to build a report for some tests made with TestNG framework. Is there any way to get all asserts used within a test method?
AFAIK, nope. What you can do is to have your defined assertions, which you can use in your tests and which, when executed would increment some counter or get added to some collection which you can reference in your report listeners.
The latest version of TestNG allows you to create your own assertions, details here.

How do I control the order of execution of tests in Maven?

I need to run tests in order. I fail to find this adequately documented anywhere. I would prefer to do this from command line. Something like
mvn -Dtest=test1,test2,test3,test5 test
How do I do this?
You can't specify the run order of your tests.
A workaround to do this is to set the runOrder parameter to alphabetical.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<runOrder>alphabetical</runOrder>
</configuration>
</plugin>
and then you need to have rename your tests to obtain the expected order.
However it isn't a good idea to have dependent tests. Unit tests must be fIrst.
You could create a test suite which runs all of your tests and run that.
With junit 4: -
#RunWith(Suite.class)
#Suite.SuiteClasses({Test1.class,
Test2.class,
Test3.class,
Test4.class,
Test5.class
})
public class TestSuite
{
}
That will run them in the correct order.
If you really need an order of your tests than you should use testng instead of JUnit where you can define dependencies between tests and based on that a particular order of tests. I know that in practice their are times where the independent paradigm does not work.
There is a Maven Surefire plugin that allows you to specify test order.
On the off chance that your tests need to be run in order because they depend on each other, I would strongly recommend against that. Each test should be independent and able to be run by itself. And if each test is independent then it doesn't matter what order they run in. Having independent tests also means you can run a single test repeatedly without having to rerun the entire test chain. This is a huge time savings.
The runOrder parameter of the surefire-plugin will help, but it will only help if the classes are executed in order. It does not help to order the test methods within one class (surefire-plugin 2.22.2, junit 5.6.1). To achieve the order within a test class with jUnit use jUnit's feature of controlling the test order within a class by #TestMethodOrder(MethodOrderer.Alphanumeric.class) (jUnit 5).
If your unit tests need to be ran in a specific order, it's probably because your tests are badly designed, or your app is badly designed. Your unit tests should be independant from each other.