I have a Java application that uses buildr. My unit test are located in : src/test/java
The buildr doco talks about support for integration tests but where do I put my integration tests? how to I separate them from unit tests?
Each buildr subproject can have either unit tests or integration tests. I use unit tests in each subproject that actually builds an artifact and then a separate subproject just for integration tests.
I ended up defining a subproject for the integration tests. See below:
integration_layout = Layout.new
integration_layout[:source, :test, :java] = _('src/integration/java')
define "integrate", :layout => integration_layout do
test.with TEST_CLASSPATH
test.using :integration
integration.setup { Rake::Task['my_project:jetty_start'].invoke }
integration.teardown { Rake::Task['my_project:jetty_stop'].invoke }
end
I can then run the integration tests with the following command:
buildr integration
Related
I'm using JenkinsFile to manage our pipeline.
Since, we integrated maven-failsafe-plugin to run the unit tests and integration tests separately, we get separate result reports from both plugins.
This is how i configured the same in JenkinsFile in the post section:
junit testResults: '**/target/surefire-reports/*.xml, **/target/failsafe-reports/*.xml'
I would have expected Jenkins to show unit tests and integration tests separately, but unfortunately, Jenkins merges the results of both XML and there is no clear distinction between unit test and integration tests.
We want our builds to have a separate view on Integration tests results.
Is there anyway to do that with Jenkins?
I dont know a way how to do that in Jenkins itself, but maybe a workaround. After you use mvn clean verify you can use mvn site in a jenkins pipeline and build a HTML representation. These testresults are splitted between failsafe and surefire and you can open it directly from your workspace and look into the diffrent results from failsafe and surefire plugin.
Maven project has following folder structure:
src/main/java
src/main/resources
src/test/java
src/test/resources
If we navigate to maven project folder and then hit mvn clean test. This command will clean the project then build the project and run the tests
But, I didn't understand which tests it will run.
In which folder, of above folder structure, it will look for tests to execute? (Maybe src/test/java?)
Based on what, it will determine if particular method is test? (Maybe TestNG or JUnit test methods?)
Maven uses Surefire test plugin by default.
It looks for the unit tests in src/test/java as you correctly guessed in your question.
Surefire supports several unit testing frameworks, such as JUnit and TestNG or POJO tests. So if you have both TestNG and JUnit tests in src/test/java and have appropriate test dependencies for both frameworks in your pom.xml both JUnit and TestNG tests will be executed.
I have a multi module maven project
JAR module A
JAR module B depends on A
WAR module C depends on B
Each of the modules has three types of tests.
Unit tests that don't depend on any external resources being present
Database integration tests that only depend on external database being present
REST api tests that depend on the application running in tomcat before they can run
I would like maven to execute the tests in the following order.
All unit tests for modules A,B,C
All database tests for modules A,B,C
All REST api tests for module A,B,C
I know how to use the surefire and the failsafe plugins to separate the unit tests from the integration tests. However i don't know how to make the failsafe plugin split the integration tests into two groups the database tests and the rest api tests.
How can the failsafe plugin be configured to split integration tests into groups that can be run separately?
All tests are written with TestNG.
I have some junit tests and htmlunit integration tests in one of my maven project. The problem is that my integration tests are not getting executed when I run
mvn clean test
Junit Tests Here:
webstore\src\test\java\com\istore\dao\AddressTest.java
Integration Tests Here:
webstore\src\test\java\com\istore\presentation\htmlunit\PageTests.java
How does mvn determines that AddressTest.java should execute and other one should not?
http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#includes describes the expected filepatterns.
btw, if you are talking about integration tests, have a look at the maven-failsafe-plugin. It uses *IT.java as file pattern
The problem is suffix Tests, should be Test in your PageTests.java!
By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:
**/Test*.java - includes all of its subdirectories and all java filenames that start with Test.
**/*Test.java - includes all of its subdirectories and all java filenames that end with Test.
**/*TestCase.java - includes all of its subdirectories and all java filenames that end with TestCase.
Maven Failsafe Plugin is for integration tests and uses suffix IT. To invoke integration tests with Failsafe Plugin use
mvn verify
References:
Inclusions and Exclusions of Tests in Surefire
Inclusions and Exclusions of Tests in Failsafe
Problem resolved after executing following command:
mvn failsafe:integration-test
In our project, we have a plenty of unit tests. They help to keep project rather well-tested.
Besides them, we have a set of tests which are unit tests but depends on some kind of external resource. We call them external tests. For example, they can sometimes access web-services.
While unit tests are easy to run, the integrational tests couldn't pass sometimes: for example, due to timeout error. Also, these tests can take too much time to run.
Currently, we keep integration/external unit tests just to run them when developing corresponding functionality.
For plain unit tests, we use TeamCity for continuous integration.
How do you run the integration unit tests and when do you run them?
In our project we have separate suite for regular/plain unit tests and separate suite for integration tests. The are two reasons for that:
performance: integration tests are much slower,
test fragility: integration tests fail more often due to environment-related conditions (give false positives).
We use TeamCity as our main Continuous Integration server and Maven as build system. We use the following algorithm to run the tests:
We run unit tests at within Eclipse IDE and before every commit.
We run unit tests automatically after each commit on TeamCity agents using Maven's mvn clean install
We run integration tests automatically on TeamCity agent after "main" build is completed.
The way we trigger integration tests execution is by configuring TeamCity's integration.tests task to be dependent on "main" continous.build task, see here for details: http://confluence.jetbrains.net/display/TCD4/Dependencies+Triggers
We run only integration tests (excluding unit tests) by:
using separate directory named
"src/it/java" to keep integration
tests,
excluding by default this source folder from maven-surefire-plugin configuration (configuration/excludes element),
using Maven profile called "integration" to exclude regular unit tests and include tests from "src/it/java" (this profile is configured by passing -Pintegration in integration.tests task).
We're using Maven2: maven-surefire-plugin to run unit tests (in the test phase) and maven-failsafe-plugin for integration tests (integration-test phase).
By default, all tests run when the project is built, however integration tests can be turned off using profiles.
In many cases integration tests are the part of the module, n some cases there are also dedicated modules which only do integration tests.
One of the teams also uses Fitnesse for acceptance testing. These tests are also in dedicated modules.
We're using Hudson for CI.
We run all the tests in one huge suite. It takes 7 minutes to run.
Our integration tests create mock servers. They never time out -- except when the test requires the server to time out.
So we have the following kinds of things. (The code sample is Python)
class SomeIntegrationTest( unittest.TestCase ):
def setUp( self ):
testclient.StartVendorMockServer( 18000 ) # port number
self.connection = applicationLibrary.connect( 'localhost', 18000 )
def test_should_do_this( self ):
self.connection.this()
self.assert...
def tearDown( self ):
testClient.KillVendorMockServer( 18000 )
This has some limitations -- it's always forking the client mock server for each test. Sometimes that's okay, and sometimes that's too much starting and stopping.
We also have the following kinds of things
class SomeIntegrationTest( unittest.TestCase ):
def setUp( self ):
self.connection = applicationLibrary.connect( 'localhost', 18000 )
def test_should_do_this( self ):
self.connection.this()
self.assert...
if __name__ == "__main__":
testclient.StartVendorMockServer( 18000 ) # port number
result= unittest.TextTestRunner().run()
testclient.KillVendorMockServer( 18000 )
system.exit( result.failures + result.errors )
To support this testing, we have a number of mocked-up servers for various kinds of integration tests.
We use Jenkins to run our tests automatically.
Be careful of differencing between Unit and Integration - Tests. It is confusing to talk about "Integration Unit Tests"
Maven offers good support to distinguish between Unit and Integration Tests-
Failsafe & Surefire Plugin.
From Apache Maven Project:
The Failsafe Plugin is designed to run integration tests while the Surefire Plugin is designed to run unit tests.
(see: http://maven.apache.org/surefire/maven-failsafe-plugin/)
You need to configure these Plugins in your pom.xml
You then only use mvn test - to run unit tests or mvn verify to run integration tests.
Unit test should run periodically f.i. every 15 min.
Integration Test, normally take long, and should run f.i. every 24 hours.
Hope that helps others.