Running Tests in a separte JVM - java

Is there a clean way to implement running #Testmethods in each own separate jvm.
I got surefire setup to fork each TestClass but I need to run methods.
Each should start in it's own separate JVM. Ideally through an annotation so I could parametrise which tests can fork testcases and which one test methods

You can achieve forked test execution using Maven's Surefire Plugin.
See the section about forked test execution in http://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html
This is the example from the page above illustrating how to create 3 forked executions:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
<systemPropertyVariables>
<databaseSchema>MY_TEST_SCHEMA_${surefire.forkNumber}</databaseSchema>
</systemPropertyVariables>
<workingDirectory>FORK_DIRECTORY_${surefire.forkNumber}</workingDirectory>
</configuration>
</plugin>

Related

Increase the logging level on Apache camel tests [duplicate]

I'd like to see the stacktrace of unit tests in the console. Does surefire support this?
A related problem that I found is that surefire in recent versions apparently sets trimStackTrace to true by default (rendering most stack trace in failed tests useless), which is quite inconvenient.
Setting -DtrimStackTrace=false or defining
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
solved this.
You can use the following command to see the stack trace on console instead of report files in the target/surefire-reports folder:
mvn -Dsurefire.useFile=false test
To extend the answer given before, you also can configure this behavior in your pom.xml:
..
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<useFile>false</useFile>
</configuration>
</plugin>
..

How to build maven artifact that run test suites? How 'sbt test' works?

I'm scala developer, so i'll try to explain my thoughts with sbt example but i belive java developers can understand me
Traditional scala/java project structure is like follow:
src ->
- main
- test
- it
test folder is conceptually for unit testing (for testing app logic). So you can run 'sbt test' and will test over your application. This code must be close to your application.
may be that was offtopic, here is my question:
is it possible to build jar artifact that would run all my tests? I don't want to start sbt just in order to run my tests. I want to build many jar artifacts with integration test suites
java my-super-service-it-case1.jar
this would give me exit code 0 if tests have passed
java my-super-service-it-case2.jar
so on...
I belive i need to run test within main block..
I would not recommend to mix any of your tests within the main functionality of your program, there is a very good plugin for exactly what you want to do, Maven Surefire Plugin and it should be working fine with Scala but have not ever messed around with Scala my self.
It would be to much to write out what you need here but it's well covered in the documentation and a lot of examples. You can specify different goals which has includes and excludes so you can for example just execute Unit tests or Acceptance Tests with for example:
mvn verify -P acceptance-tests -Dbuild.env=sit
Which has following profile configured in the POM.XML file
<profile>
<id>acceptance-tests</id>
<activation>
<property>
<name>env.BUILD_STAGE</name>
<value>ACCEPTANCE</value>
</property>
</activation>
<build>
<plugins>
<!-- skip unit tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- run acceptance tests (during integration-test phase) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*AT*.*</include>
</includes>
<excludes>
<exclude>**/*Test.*</exclude>
<exclude>**/*IT.*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
All my Acceptance Tests are named with AT like, myTestAT.java

Execute TestNG.xml from Jenkins (Maven Project)

My Project dosnt have a bin folder
Have tried the following:
D:>java -cp "Pathtolibfolder\lib*;Pathtobinfolder\bin"
org.testng.TestNG testng.xml
Any Ideas?
If you are using Maven then i would simply use the surefire plugin and run tests as part of the build:
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>RELEASE</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
...
</plugins>
Just specify the path to your testng.xml and take advantage of it if you can use this kind of configuration.
It allows for tons of parametrization and i have used it extensively in my projects.
Check out this tutorial to get a hang of it: http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html

How to run whole test cases sequentially in maven using testng?

I want to run my whole test cases sequentially in java maven using testng.
How to run test suite sequentially?
Use Maven Surefire Plugin with TestNG (Using Suite XML Files)
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
[...]
</plugins>
And to know how to configure the testng.xml please follow TestNG documentation.
Hope these information will guide you properly.

How do I run a junit test only after the build?

How do I run a Junit test case after the build is complete. I have a piece of code that looks at autogenerated files by Maven such as Manifest.MF. Now, while running a clean build, this test will fail because the file hasnt been generated yet.
Is there any way for me to run this test after the build is complete?
I assume you are looking for maven-failsafe-plugin which is intended to run integration tests which are after the packaging phase where all stuff has been generated.
You need to add the following to your pom file:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
Now you have to name your test according to the naming schemata like XyzIT.java which will be picked up by maven-failsafe-plugin and will run this test after the packaging phase. This can be achieved by:
mvn clean verify
include a dependency of maven-surefire plugin in pom.xml, which runs junit tests automagically if you followed junit conventions properly

Categories

Resources