TestNg test not running with maven build - java

I have TestNg unit tests which is supposed to run with my maven clean install.
I don't have any test-suite.xmls in my pom to run testes. Expectation is to run all my test files without any configuration with the maven build.
But this is not happening.
My test class goes like this
public class CreateUtilty{
#Test
public void testScope(){
Creationutiltiy.create("myApp");
// remaing code
}
}
What could have I done wrong ?

Running testNG as you are with no configuration, the surefire plugin expects your test classes to end with Test. Try changing your test class name to CreateUtilityTest and it should be picked up.
The documentation for the maven surefile plugin contains useful information to help you get started.
To find out more about how to include/exlude tests based on naming convention read this.

Try execute your class with following syntax
mvn -Dtest=CreateUtilty test

Related

Maven / Spring boot project - how to skip #SpringBootTest

In my project there are many tests marked with #SpringBootTest which I don't regard as unit tests and rather as integration tests. So I would like to run only the unit tests when I execute:
mvn clean install
actually I want to run this command as part of pre-commit git hook but #SpringBootTest makes it longer to finish execution.
Is there a way to exclude the tests marked with #SpringBootTest? May be there is a pattern we can pass to maven that excludes/certain tests. Or may be write a test suite that includes the spring boot tests.
I did google search to achieve the above but don't have much luck.
Is there even a better way?
#Update: Constraint is maven pom file can't be modified.
#Update2: I have a solution that looks promising:
1. Use #Category("IntegrationTests") for #SpringBootTests tests.
2. Create TestSuite with excludeCategory:
#RunWith(CategoryRunner.class)
#ExcludeCategory("IntegrationTests")
public class TestSuite {
}
3. From mvn command line, run only TestSuite.
I am not sure this is the best. Appreciate anyone's better approach.
If you have different kinds of tests, and want to be able to specify which tests to run, you can do that with #Conditionals or with #Profile.
Examples:
#ConditionalOnProperty("test.run.integration") The class will only be loaded by Spring when property test.run.integration is defined and not false.
#Profile("integrationtest") The class will only be loaded by Spring when profile integrationtest is active.
If you're on JUnit 4, use #IfProfileValue annotation on the test class or method.
Example:
#IfProfileValue(name ="spring.profiles.active", value ="IntegrationTests")
If you're on JUnit 5, as you should be by this time, use #EnabledIf or #DisabledIf.
Example:
#DisabledIf(
expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
reason = "Disabled on Mac OS"
)
See the docs for more details.
try either
mvn clean install -DskipTests
or
mvn clean install -Dmaven.test.skip=true
For more options refer to below links
https://mkyong.com/maven/how-to-skip-maven-unit-test/
https://www.baeldung.com/maven-skipping-tests

maven -Dsurefire.rerunFailingTestsCount with parameterized tests

I want to re-run failed tests with maven. I'm using surefire.rerunFailingTestsCount for this:
mvn '-Dtest=LoginTest#loginAsValidUser' '-Dsurefire.rerunFailingTestsCount=1'clean test
However, after tests fail, they are not found again by junit when trying to re-run:
org.junit.runner.manipulation.Filter.initializationError(org.junit.runner.manipulation.Filter)
Run 1: Filter.initializationError » No tests found matching Method loginAsValidUser[...
Run 2: Filter.initializationError » No tests found matching Method loginAsValidUser[...
Run 3: Filter.initializationError » No tests found matching Method loginAsValidUser[...
According to maven documentation, test method can be also indicated with [*], so I tried
mvn '-Dtest=LoginTest#loginAsValidUser[*]' -Dsurefire.rerunFailingTestsCount=1 clean test
and different variations of specifying test class/methods name, but outcome is the same.
Any ideas what is causing this?
maven-surefire plugin version is 2.19.1, junit version is 4.12, junitparams version is 1.0.5.
Test class looks like this:
#RunWith(JUnitParamsRunner.class)
public class LoginTest {
#Test
#FileParameters(value = "src/main/resources/login_data.csv")
#TestCaseName("{method}[{index}]")
public void loginAsValidUser(String username, String password) {
//test
}
}
UPDATE: I found surefire bugreport for similar situation but that was fixed. I took sample code that demonstrated the bug and run it, worked well. Then I changed runner to junitparams runner and got same error a described above. I guess this may be a bug with runner, so I opened issue on their github.

Using Surefire to execute TestNG/Selenium tests in a jar

Our project has a suite of Selenium tests that we are currently packaging into a jar with the intent of Jenkins running the tests as a build step. We install the jar into the local repository of a client VM that is configured to have Selenium point back to Jenkins as the host. The trouble we are running into is figuring out a way to get Maven/SureFire to find the Selenium/TestNG tests in the jar we installed. We have a pom with all the dependencies that the tests require on the client, including the jar of tests itself, but when we we run "mvn test" no tests are found. Clearly we are missing something here, any ideas? Thanks.
surefire by default look for file names like Test*.java , *Test.java , *TestCase.java and executes them. if your test doesnt follow any of these pattern then you have to include them explicitly. refer here
Create a main method calling testNG main method and pass your main methods args[] to testNG Main method. Now your test jar will have main method of its to trigger test cases of your desire. You can pass the same paramteres as TestNG to your jar file like testng.xml file or -testClass classapath etc... see example below.
public static void main(String args[])
{
org.testng.TestNG.main(arg);
}
now you just need to create a xml file of test classes and in jenkins use windows batch command to call your jar file on remote machine with required testNG parameters.

Maven Surefire: run a single unit test

I know that it's possible to run a specific test class with -Dtest=MyTest. But is it possible to run a specific test within that class?
I.e. if MyTest defines testFoo() and testBar(), is there a way to specify that only testfoo() should be run?
I'm aware that it's trivially easy to do this in an IDE, but I occasionally need to run tests on the command line on another server.
From Running a Single Test Using Maven Surefire Plugin
With version 2.7.3, you can run only n tests in a single Test Class.
NOTE : it's supported for junit 4.x and TestNG.
You must use the following syntax
mvn -Dtest=TestCircle#mytest test
You can use patterns too
mvn -Dtest=TestCircle#test* test
It will be available as of Surefire 2.8, see SUREFIRE-577
Don't think its available. You can work around it by passing some system properties & ignore execution of tests based on the property value. However it does not seem to add a great value add. There is also TestNG which offers additional features.
http://maven.apache.org/plugins/maven-surefire-plugin/examples/testng.html
To execute one Test at a time, run mvn test
mvn -Dtest=MyUnitlTest test
To execute one Test at a time and a specific method from it:
mvn -Dtest=MyUnitTest#method test
where MyUnitTest is the name of your test and #method is the name of your method.
Execute tests with surefire:
mvn surefire:test

TestNG Ant tasks vs Surefire

I was wondering how different surefire is when executing TestNG than TestNG ant tasks? The reason is that I am seeing consistent difference in behavior when trying to run a TestNG test that extends a JUnit test base (this is a workaround to run JBehave tests in TestNG described here: http://jbehave.org/documentation/faq/). Surefire detects my test as a JUnit test incorrectly (probably because its base is TestCase), while the Ant tasks run perfectly. Can anyone provide an insight into how TestNG handle both cases?
The test looks as follows:
public class YourScenario extends JUnitScenario {
#org.testng.annotations.Test
public void runScenario() throws Throwable {
super.runScenario();
}
}
The short answer is that the ant task is part of the TestNG distribution, so it's part of our tests and I always make sure that it remains up to date with TestNG.
Surefire is developed as part of the Maven project, and as such, it sometimes lags behind (and just like you, I have sometimes encountered bugs when running my tests with Surefire that didn't happen when running from the command line/ant/Eclipse).
I'll bring this question to the Maven team's attention, maybe they'll have more to say.
This looks to be a known bug: http://jira.codehaus.org/browse/SUREFIRE-575.
Have you tried using a TestNG XML suite definition instead of Surefire's automatic test case detection?

Categories

Resources