testing multiple classes using test suit on junit5 gets pending - java

I've written multiple test classes to test my methods using junit 5.
all the test classes pass successfully when I run them indivually
but when I try to run them all at a time using a test suite as shown below, some of my tests get pending and the testing won't finish. it doesn't even jump to test other classes
as all the methods pass successfully, I don't think if there's any problem with the class ParametrizedMethodTest
I'm using junit-platform-runner version 1-6-2

From the current JavaDoc:
Please note that test classes and suites annotated with
#RunWith(JUnitPlatform.class) cannot be executed directly on the JUnit
Platform (or as a "JUnit 5" test as documented in some IDEs). Such
classes and suites can only be executed using JUnit 4 infrastructure.
In other words, JUnit 5 does not support test suites in the way you want to do it in your example. If you want to run all your tests classes just select the package and choose Run Tests from the context menu.

Related

Does TestNG have a test collection functionality similar to pytest --collect-only with hooks?

Pytest provides the ability to run pytest on a test repository using a --collect-only option that simply outputs the tests it finds based on current configurations.
Additionally, there are hooks that can be implemented that affect what happens during various phases of the collection, such as pytest_collection_modifyitems.
I'm wondering if there's an equivalent collection/hook system available for Java tests using TestNG.
TestNG can be instructed to look for all #Test methods within a specific package and everything inside.
But by default, after TestNG discovers the tests, it executes them.
So in order for your use case to be done, you would need to do the following:
Create a testng suite xml file, that refers to the top level package com.foo.bar for e.g., wherein all the test classes are residing inside either com.foo.bar package or inside a sub-package.
Run TestNG with the JVM argument -Dtestng.mode.dryrun=true which causes TestNG to simulate as if the tests were run without actually running them.
Using both these two things, your use case should be satisfied.

Running JUnit5 Test in Parallel But Want To Leave Some Tests Sequential

I have a project that we have many Junit tests. We just did a large migration from JUnit4 to JUnit5. We would like to run most of the tests in parallel but have a couple that need to be ran sequentially. Is there any way to use JUnit5 and run tests both ways?
The reason I ask is that I have 4 tests that load a database into memory and I am loading data into this database. Then I run tests on that database. These are the four tests I need to run sequentially and cannot run in parallel.
You most likely want to use #ResourceLock annotation on tests
https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-parallel-execution-synchronization
You might take a look at #Isolated annotation
https://junit.org/junit5/docs/5.7.1/api/org.junit.jupiter.api/org/junit/jupiter/api/parallel/Isolated.html.
It guarantees that test-class won't run in parallel with other classes.
According to Parallel Test Execution and Single Thread Execution:
Since of maven-surefire-plugin:2.18, you can apply the JCIP annotation
#net.jcip.annotations.NotThreadSafe on the Java class of JUnit test
(pure test class, Suite, Parameterized, etc.) in order to execute it
in single Thread instance. The Thread has name
maven-surefire-plugin#NotThreadSafe and it is executed at the end of
the test run.
So you could annotate your test classes with #NotThreadSafe in order to get them executed on 1 same thread (named maven-surefire-plugin#NotThreadSafe).
<dependency>
<groupId>com.github.stephenc.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0-1</version>
<scope>test</scope>
</dependency>

How to implement a custom runner in JUnit5

Is there some way to have complete control over execution of test methods (including before/after methods) in JUnit5, similar to JUnit4 #RunWith annotation)?
I'm trying to build a JUnit5 Arquillian extension, but since Aquillian basically needs to execute each test in a container, I'm coming to a problem when running Arquillian from a Junit5 extension.
My code is here: BasicJunit5ArquillianTest.java
The test should run all methods (including before/after) in a separate container, which can be a separate JVM, remote or embedded server or anything isolated. My extension runs the test method from beforeEach hook, using Arquillian to transfer the test class and run it in a container using LauncherFactory.create(), collect test results and transfer them back.
The problem is that the test methods are executed twice - via normal JUnit5 execution and via my Arquillian extension from a beforeEach hook. I'd like to run tests only via Arquillian and skip normal execution of methods.
Is this possible in a JUnit5 extension?
Or I need to create a custom test engine, possibly extending the Jupiter test engine?
There is no extension point (yet?) that allows you to define where or how tests are run. This is already true for threads, which means there is no way to run them on JavaFX application thread or Swing EDT.
You might have to go deeper and implement an engine but that means that users have to choose between writing Arquillian tests or writing Jupiter tests.
UPDATE: In a newer version of JUnit 5 released since this answer was accepted, JUnit 5 now provides the InvocationInterceptor extension point, which is exactly what is needed to implement a custom runner as an extension, which has a full control over how the tests are executed and can even replace the body of the test method with something completely different (e.g. run the test in a different JVM and return the result).

JUnit setup for all tests

I need to setup a database in my tests (schema and some test data), this takes quite a bit of time, and as such I prefer to have it done once for all tests that are being run, and reset so that any chanages to the DB are rolled back between tests.
I'm not sure though which JUnit facilities should be used for this.
It seems like I can set a #BeforeClass/#AfterClass on a test suite, but than I can't run individual tests anymore.
Is there some way to add a setup/teardown for all tests that will run even when only executing a subset of the tests and not a specific suite? (For example NUnit has SetUpFixture)
I guess the transactions/truncation of the DB can be done using JUnit Rules...
You can use in-memory databases like HSQL or H2 to speed up test.
To roll back, you can use transactional feature.
Is there some way to add a setup/teardown for all tests that will run even when only executing a subset of the tests and not a specific suite?
For this, you can create a super class which is extended by other test classes. In super class, you can set up to setup/teardown.

JUnit Test Runner that creates tests just before running them

I use JUnit 3.x TestRunner that intantiates all tests at once before running them.
Is there a Test Runner available that would create each test (or at least each test suite's tests) just before running them?
I can use JUnit 4.x runners but my tests are 3.x tests.
In JUnit 3 you'd need to write your own TestSuite class that delayed instantiation of the tests in the suite.
You are probably doing it wrong.
Each unit test should be self-contained and not depend on any other test results.
Otherwise when one of the tests break it will break all the tests that depend on it. So you will see a lot of errors without easy way to understand what is the actual cause. On the other hand if all unit tests are independent a broken test is extremely easy to debug and fix.
EDIT: I am assuming the reason you ask the original question is because you have some dependencies in your test. If I am wrong please ignore this answer :)

Categories

Resources