I am working on a Maven project and am trying to exclude some tests that should not run in the build phase. However, all the guides I can find give instructions for how to exclude either through command line arguments or with the Surefire plugin. Isn't there a way to exclude the file through the pom.xml without using Surefire, as I'm not using Surefire to run the unit tests in the first place? Where would this exclusion go?
Surefire is the name of the framework for executing tests (it's an abstraction layer for the specific tool). It support JUnit, TestNG and others. To exclude tests by pom.xml, have a look at http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
Related
I am working on understanding Maven and I'm learning about building your Java app with it.
So when I do a :
maven package
It does build my jar as expected but I see in the output console that Maven does build tests (it always say that the test a run and there are no failure).
I researched on the web about that and learned that Maven use a plugin called Maven Surefire. But I can't understand what does that plugin do to my code, what does the tests "means" ? What does the tests do with my code and how it works behind the console ?
The Maven surefire plugin runs the tests you have written. These are usually in the src/test/java folder. If you have none, the plugin does nothing.
Is this only one question? :D
So. Different things are going on.
You create an application with Java. To test the single components / packages / classes that you create most people use JUnit or TestNg. You usually have dedicated test classes that verify your production code behaves as intended without you clicking through all the things on every change.
When you now use maven to run your build the pom.xml file defines a packaging - in your case "jar" since you create a jar file. The packaging defines what set of default plugins run in the defined maven phases. You probably recognize package here. Maven executes all phases up to package and the registered / configured plugins.
To execute those tests maven provides the surefire plugin which supports running JUnit or TestNg tests. If you follow the directory conventions your tests reside in src/test/java and the surefire includes naming convention maven will execute those tests in every build (as this is the best practice). If you also want to write integration tests then there is the failsafe plugin. That plugin is not enabled by default and runs in different maven phases.
So the tests just run your production code - in fact they just do what you implement in the tests. They don't alter it in any way.
The maven introduction documentation has step by step explanations: Maven in 5 Minutes and the Getting Started Guide.
Starting from scratch this is probably a lot. So don't rush this. The build setup and test setup are very important things to have.
I have JUnit tests that have a rest-assured dependency.
How can I run these in Gradle?
I have no idea what you mean by 'rest assured' tests but I'll assume you just want to run either JUnit or TestNG tests in gradle. You should really remove any maven references from your question to avoid confusion... this is a simple Gradle question and has nothing to do with Maven.
The following line in your build.gradle
apply plugin: java
Adds the java plugin to your build which adds appropriate test tasks to your build. Please see the java plugin documentation for configuring JUnit / TestNG
I use Gradle for building my project and I have some trouble between cucumber and gradle. For example:
I have a feature file, which I run with JUnit RunWith annotation. But if I want to run 1 scenario by tag, I write this:
-Dcucumber.options= --tags #1 #2 and try to run it.
But Gradle doesn't see this command and runs all tests. So, how can I correctly apply cucumber plugin into Gradle?
So, I found one way to do it. All we need is to use testCompile with JUnit, not cucumber-junit and all is right.
Does Maven surefire require a testng.xml file to run testng classes and methods? I have a multi module reactor setup in maven. Testng and some custom tools are located on one module and our tests are located on another module. Our structure is like this
/pom.xml
/testng-utils/pom.xml
/testng-utils/src
/tests/pom.xml
/tests/src
The tests module has a dependency on testng utils. testng-utils module brings in testng and hamcrest. Surefire is a plugin in tests/pom.xml.
I use testng annotations inside my tests, and I don't use an testng.xml file, I realize I'm providing very little information so I'm not expecting anything too in depth for an answer, just maybe a hint as to why my tests aren't being found.
Does anyone use a setup at all similar to this, and invoke testng without the use of a testng.xml file through surefire? I can try to include more information if anyone wants it if I'm able, but this is for the company I work for so I can't put much.
Thanks!
As long as you place your tests under src/test/java directory and follow the *Test.java naming convention surefire should be able to pick them up without any further configuration.
Surefire + TestNG
I have a multi-module maven project. In every module there are unit tests. When I make clean install tests run before every module and if all tests in one module are success it build successfully. If one test failure all other tests in that module run successfully (or some run successfully, other failed). The build of module in what the first failure unit test is placed failed. Other modules are skipped.
I want such thing: first to run all unit tests in all modules, and after that if there is no failed tests build all modules, or if there is one or more failed tests in one or mode modules skip building of all modules. Can you help me with it?
run:
mvn clean test
mvn install -Dmaven.test.skip=true
note, if you have inter-module dependencies (which i assume you do), you probably can't really do this, as you will need to build the dependent jars before you can run the tests in the other module.
AFAIK its impossible in maven. You are trying to change a maven build lifecycle which is not allowed in maven. However there are a couple of configuration parameters you can pass to maven and this will affect the testing.
mvn install -Dmaven.test.skip
This won't run unit tests at all
mvn install -Dmaven.test.failure.ignore=true
This will cause maven to not stop and proceed the module building process even if there were failures during the test phase.
Hope, this helps
The problem is:
the modules probably have dependencies upon each other, and to resolve those dependencies, you have to build the modules in order, or they won't compile. So there's no sane solution to your problem.
Insane solutions would somehow aggregate the sources (and external dependencies) from all child projects and run compile and test on that conglomerate, but it would be such a monstrous hack that I'm glad they didn't do it.