I run test cases from maven, that is from command line. Is there a way to see a reason why test failed in terminal output? I've tried raising verbosity in testng.xml to 4,hoping that it will activate TextReporter, which seems like class that prints, well, text reports, but it didn't help.
Try mvn -Dsurefire.useFile=false ... test or corresponding setting in surefire plugin configuration.
Related
I want to run the build for Apache James which has a huge test suite that is running very long due to tests that irrelevant to me, e.g. tests concerning RabbitMQ. Thus I'd like to exclude those and I want to do so from the command line (not by editing POMs). I'm using Maven 3.6.3 on Java 11 OpenJDK. The project uses JUnit5 and maven-surefire-plugin 2.22.2.
Now, I would expect the following to work:
For example, to run only test methods in the org.example.MyTest test
class you can execute mvn -Dtest=org.example.MyTest test from the
command line.
But it doesn't work. In fact, as soon as I set the test parameter to anything else than an empty string, all tests will be skipped. I tried some of the syntax that is supposedly supported...
mvn package -Dtest=*
mvn package -Dtest=".*"
mvn package -Dtest=\!SomethingFishy
mvn package -Dtest='!MavenHeadache'
mvn package -Dtest='!%regex[.*HelpMe.*]'
...but the result is always the same:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test
(default-test) on project testing-base: No tests were executed!
(Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
I'm running the package goal, as it does the full build, but test behaves the same. Also tried specifying -Dincludes=... / -Dexcludes=..., which had no effect at all: All tests are executed regardless and the parameters don't even show up in the output of mvn -X .... This behavior doesn't change when update maven-surefire-plugin to the latest version which is 3.0.0-M5.
Do I understand something wrong here? How can i specify inclusions/exclusions in this setup?
Update: It looks like this is caused by nested projects and/or James' project structure in particular. If I enter a "leaf project", e.g. core, then inclusions/exclusions begin to work:
cd core
mvn test -Dtest=HostTest # will only run HostTest, as expected
mvn test -Dtest=\!HostTest # will run all tests but HostTest, as expected
As suggested by RobertScholte, I have looked at the maven-surefire-plugin configuration, but couldn't find anything that seems to be related to this behavior.
The error message tells what you need to know: in project testing-base which is the first to run, there's no test matching your pattern, so it fails to ensure you won't have a false impression of success.
It then suggests to use -DfailIfNoTests=false option to let maven ignore modules that don't have any test matching the pattern (it's probably what you need).
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
I'm confused about the correct way to set a property for some unit tests via the command line when using Maven. There are a number of questions (e.g. Specifying Maven memory parameter without setting MAVEN_OPTS environment variable, Is there a way to pass jvm args via command line to maven?, How to set JVM parameters for Junit Unit Tests?) that touch on this subject but none have the answer I'm looking for.
I want to set the property java.util.logging.config.class to some value but I don't want to set the MAVEN_OPTS environment variable.
I can configure the surefire plugin in my pom file with the property:
<argLine>-Djava.util.logging.config.class=someClass</argLine>
so that it is set every time the test phase is run.
However, if I remove the setting from the pom file and add the following to the command line:
mvn package -DargLine="java.util.logging.config.class=someClass"
then the following error in the test phase is reported and the build fails:
Error: Could not find or load main class java.util.logging.config.class=someClass
If I run the following from the command line:
mvn package -Djava.util.logging.config.class=someClass
then the following error is reported at the beginning of the build but the build and tests are successful:
Logging configuration class "someClass" failed
java.lang.ClassNotFoundException: someClass
I don't really understand the behaviour. Can someone enlighten me?
Yes, you should have
mvn package -DargLine="-Djava.util.logging.config.class=someClass"
Notice the addition of -D inside the argLine.
Let's explain why. argLine is indeed an attribute of the maven-surefire-plugin with the corresponding user property argLine. This means that you can set this property directly on the command line with -DargLine. But then, the value of that property is -Djava.util.logging.config.class=someClass. This is exactly what you had when you configured the plugin in the POM directly with
<argLine>-Djava.util.logging.config.class=someClass</argLine>
Additionally, when you call
mvn package -Djava.util.logging.config.class=someClass
then you are not setting the argLine property. You are adding a system property.
Regarding your approach to configuring tests
If you want to pass in configuration to your tests, use for example a file in src/test/resources/myTestConfig.xml. Or use the enhanced features of Test-ng. Your tests will have no value on the centralized build server, or those who want to run/test your code, where config values can't be changed (easily).
The recommended usage of command-line arguments for Maven is for configuring the Maven plugins, build environment and Java config for the build.
Use as little features, config and plugins as possible. Or you'll face a ton of issues down the line, and the tweak-all-you-want-A-standardized-build-doesn't-mean-sh*t-for-me-Gradle will feel more comfortable.
I am trying to run a JUnit 4 test from commandline. This is my current command:
java -cp C:\Users\some\.m2\repository\junit\junit\4.11\junit-4.11.jar junit.textui.TestRunner C:\Some\Path\target\test-classes\com\wicket\range\ui\MyTest.class
This gives a class not found error.
I have also tried the following:
C:\Some\Path\target\test-classes>java -cp C:\Users\some\.m2\repository\junit\junit\4.11\junit-4.11.jar junit.textui.TestRunner com.wicket.range.MyTest.class
This also gives a class not found error; what could be the issue here?
I assume your test class is under C:\Some\Path\target\test-classes (in appropriate subdirectory). Your command has only junit in it. It also need class path to the test and other dependencies.
Try
java -cp C:\Some\Path\target\test-classes;C:\Users\some\.m2\repository\junit\junit\4.11\junit-4.11.jar junit.textui.TestRunner com.wicket.range.MyTest.class
Looks like you are using Maven (saw the ".m2" in your classpath). How about this..
cd <location of pom.xml>
mvn -Dtest=MyTest test
Granted it may be a while until before it runs your test and it's not going to use JUnit's text ui runner, but it should run your test without much fuss about ClassNotFoundException. Examine files in target\surefire-reports for test results afterwards. Guess it depends on exactly what your goal is.
Otherwise, Jayan's answer looks about right to me. For his to work, I think you want to first
cd C:\Some\Path\target\test-classes
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