maven and unit testing - combining maven surefire plugin AND testNG eclipse plugin - java

could you please share your way of unit testing in eclipse ? Are you using surefire plugin, m2eclipse & maven, or only testNG eclipse plugin ? Do you combine these alternatives ?
I'm using testNG + maven surefire-plugin and I had been using the testNG eclipse plugin a year ago so that I could see the results in testNG view. Then I started using Maven, but when I do "maven test phase" using m2eclipse, there is only console output and surefire reports that I can check in browser and to choose what test suite, test, or test method can be set up only via testng.xml.
On the other hand, if you use only surefire plugin and you have some specific settings regarding classpath etc., that you rely on, then running tests via testNG eclipse plugin doesn't have to be compatible with your code. Using surefire plugin, the classpath is different - target/test-classes and target/classes - than using testNG plugin, that is using the project classpath.
How do you go about what I was just talking about?
Is it possible to synchronize "maven test" using m2eclipse and surefire plugin WITH testNG eclipse plugin and view ?
EDITED: I'm also wondering, why the Maven project ("Java build path") output folder is target/classes for src/main and src/test whereas surefire plugin makes two locations target/test-classes and target/classes
Thank you very much for your your opinions.

Lisa,
You can configure the TestNG Eclipse plug-in to "watch" a test-output directory. Point it to target/surefire-reports and you should see your TestNG view update itself a few seconds after a Maven build terminates.

I see two advantages of using the surefire plugin:
Relying on the eclipse plugin only
works that way when everyone in the
project uses eclipse
Surefire plugin
can run from build that are done from
the continuous integration server
("jenkins")
And then if you have larger more long running (performance) test suites, you probably don't want to "block" your IDE while they run.

I don't think it is true what you say with the different runtime classpath, I just checked myself both maven classpath and the one when test is run via testNG eclipse plugin and both are the same. I think that m2eclipse plugin takes care of it. It's the same even for junit testing.
So afaik, there is no restriction for using testNG eclipse plugin for development and surefire plugin for instance for continuous integration as Heiko Rupp mentions. At least I have never gotten any troubles with it.
Just use both as you like, cheers !

Related

how to run unit tests for a play framework application within intellij

I tried running unit test on the play framework java starter example within intellij but it doesn't seem to work.
I downloaded the project and imported it into intellij via the SBT import.
When I tried running the tests from intellij I got "Class not found: "UnitTest"Empty test suite"
If I run the SBT test task, the tests are running and passing.
When I go bask to running the tests from intellij after running the SBT test task, the test run and pass.
It's like intellij is not compiling the project
I am working with intellij ultimate edition version 2018.2 and have the scala plugin (v 2018.2.11)
How can I make this work?
Edit (in respose to #hce)
I upgraded intellij to the latest version: 2018.3.2 (couldn't find 2018.3.5) but I still experience similar problems.
I started with a newly downloaded starter project.
import the sbt project:
Then tried to compile and got an error:
I managed to solve the compilation problem by configuring intellij to use the play2 compiler ("Use play2 compiler for this project")
I then tried running the tests and but intellij complains it can't find the class
this is the run configuration that was created by intellij. the class name (UnitTest) is marked red...
Is it any different in your environment?
Is there anything wrong in my process?
I tested it with IntelliJ 2018.3.5 and the Scala plugin. It works well.
So the cause seems to be in the configuration of your project.
It would be helpful if you can provide your run/debug configuration and the settings.
There are many reasons.
You could try the following...
Make sure you use java 1.8 as the project jdk.
Use sbt 1.2.1
Check your run/debug configuration.
File -> Invalidate Caches/Restart...
Build --> Rebuild Project
Run --> Edit Configurations / remove Junit / Apply and restart your test. A new Junit configuration is created automatically
Edit:
After the comments of #NirBrachel, the solution was to use the sbt-shell to compile the project. (I recommend clean, compile and test).
You could automate this during import:
Why?
Info: sbt compilation for play framework 2.x disabled by default
You can try to activate Play2 framework in the Settings...
WARNING: This settings didn't work for me, because it's not saved by IntelliJ.
I didn't find out yet why.
EDIT see this link for a possible cause.
I use Scala plugin and Play Framework. I can run tests in 3 ways:
simple run 1 test from IDE (but I can't run all tests in package by this way)
run tests from sbt
run tests with use this configuration (it's allow run tests with coverage):
Sorry for my English.

Why Surefire plugin is need in Maven?

I have used Maven for a while. In my previous projects, i did not create pom.xml from scratch, and I've seen Surefire plugin always already added to POM.
At first I thought Surefire is a must for Maven to be able to search for test java files to run, however even after I remove Surefire plugin from POM, Maven is still able to find the test cases under src/test/java.
So is the purpose of Surefire to add additional features such as in:
https://maven.apache.org/surefire/maven-surefire-plugin/
but not absolutely necessary to run "mvn test"?
Thanks
Very first line you doc you provided says:
". It generates reports in two different file formats:"
And reports are very important, I assume you must have seen reports after running test to check coverage and other stats.
You have answer somewhere in your question itself :)
I was also searching for reasons why so many plugins required such as Sure-fire and fail-safe..
found that we need surefire for "test" phase to execute the UNIT TEST of an application
which is the least minimal activity we do to after compiling our code therefore surefire is omnipresent.

Cucumber tests are being skipped

I am using cucumber framework in my project. So for the reporting part, i need have started building the cucumber reports with jenkins.
So during the setup for the trail build i am facing the below issues.
All the tests are skipped, but not executing.
Can anyone have solution. I am happy to provide any other inputs required.
When using maven and cucumber you have two options to run the tests.
With maven: mvn clean install
With cucumber:-Dtest=Runnerclass test
Where Runnerclass is the class name of runner.
Include surefire plugin in your POM.xml to pick your test. if you want to run test as well, then use mvn clean install.
Refer
How to run Cucumber JVM Test in Jenkins

How to configure Testng eclipse plugin with arquillian

I have arquillian set up here with some integration tests, but so far I'm running them with:
mvn clean test -Parq-jbossas-managed
I would like to run and see the errors/successes on the Testng eclipse plugin. How do I do that?
I have tried to add a testng running config, selection a class to test, but it didn't work.
Any ideas?
Running TestNG from its Eclipse plugin or from maven are two different things!
The TestNG Eclipse plugin comes with its own version of TestNG - afaik, this isn't possible (without starting eclipse processes from you build process)
If reporting matters to you, you should have a look at ReportNG
To start a test run with your Eclipse Plugin (so you can see it in Eclipse's dedicated view), you have to select your test-class (or your test-xml) and use Run As -> TestNG test.

What is the process with Maven project to compile and test the code?

I have maven project imported in my eclipse. Now I need to start making changes to it and test it with the integration test (out of App server). Currently, the integration test is run out of server using openEJB container.
My basic question is, what is the regular process to compile, build and test with Maven?
mvn install
Maven -> Update Project.
Run my test from command line
Is it how it is done? I am specifically interested in knowing mvn install commands.
So should I do all three steps before I can test it?
Example: I just wanted to print something and see what is the output. For this I guess I need to do all these steps?
The openEJB container needs classes so it can load them.
There is a wonderful Maven quick-reference sheet at http://maven.apache.org/guides/MavenQuickReferenceCard.pdf
First, you should be aware that unit tests and integration tests are separate and are run from separate plugins and at separate parts of the maven lifecycles. Unit tests are run with surefire and integration tests are run with failsafe.
You want to run integration tests and the failsafe documentation says:
NOTE: when running integration tests, you should invoke maven with the (shorter to type too)
mvn verify
rather than trying to invoke the integration-test phase directly...
This is the best way to run integration tests directly in maven. It will run all the preceding steps necessary (eg: compile) in order to run the integration tests. It won't waste time doing an install because install happens immediately after verify.
But if you're running the tests locally, it may be a better idea to run your integration tests directly in your IDE. That will give you a much faster feedback loop.
If it is Eclipse project the most reasonable thing is to do everything not from command line but from Eclipse. Assuming you have m2e plugin installed, go to your_project->run as->Maven test and run it.
You dont need neither install nor package phase to run Maven tests, package will create a jar which is not needed for tests, install will copy this jar to local repo which is also useless. When Maven run tests it uses compiled classes from target dir and ignores project's jar if even it exists.
Yes, mvn isntall is the most popular option. It compiles, packages and tests your project.

Categories