2 variants for JUnit execution: TestRunner & JUnitCore - junit

There seem to be two approaches for invoking JUnit tests from the OS command shell:
java junit.textui.TestRunner <class-name>
and
java org.junit.runner.JUnitCore <class-name>
When do we use one versus the other?
Also, are there other ways to invoke Junit tests from the OS command shell?

JUnitCore is an entry point of JUnit - so if you want to run a test programmatically or of from some non-java script, I think, its the way to go for JUnit 4.
TestRunner is something a very old junit 3.x
Notice, that nowadays JUnit 5 is the latest available major release and it has yet another way to run the tests.
The question about different ways of running the tests from command line has been already answered Here so I can't add much to this.
However, I can comment on:
Also, are there other ways to invoke Junit tests from the OS command shell?
Nowadays in regular projects people do not run tests like this, instead they use one of build tools (Maven, Gradle for example) that among other things take care of tests.
So for example if you use maven, you can run mvn test and it will compile everything you need, including source code of tests, will take care about all test dependencies and will run all the tests with the help of build-in surefire plugin.
If you don't want to compile anything (assuming that all the code has been already compiled and all is set, you can use mvn surefire:test)
These build tools are also integrated with CI tools (like Jenkins, etc.) So this is considered to be a solved problem.
So unless you're doing something really different (like writing the IDE UI that should run test selected by user on demand or something) there is no really need to run tests with the options you've mentioned.

Related

How would I run integration tests from vivet/googleApi?

I am trying to work out how I would integrate this shared library from GitHub into my code, since it is a shared class library, for starters I just want to run the integration tests, but I cannot work out how go get the test runner to run them.
I created a console application in my main project and a reference to the GoogleMapsApiTest in the console but I am not sure how to call the tests from there to run them.
GoogleAPIClassLibrary
I had to download the gui test runner and build it from GitHub. Link to project
now I can at least run the tests, I am still not sure how to use the library but that should help at least see how it is supposed to work.
I was able to run the unit tests by downloading the NUnit source code at the link in my post and then browsing to the output dll of the class project, to load the tests apparently the gui-test runner is no longer available for download, so hopefully that will help someone else out if they run into a need for running tests in NUnit.

Forcing Eclipse to run JUnit tests in the same order that Maven runs them

I’m using Maven 3.2.3, SureFire 2.17, JUnit 4.11 and Eclipse Juno on Mac 10.9.5. I notice that when I run my JUnit tests via the command line
mvn test -Dtest=MyTest
the individual tests within the file “MyTest.java” run in a different order than when I run them in Eclipse (by right clicking the class name and selecting “Run As -> JUnit Test”). How do I get Eclipse to run the tests in the same order in which they are run on the command line?
Thanks, - Dave
The order of JUnit test runs are not guaranteed by design, as is mentioned in the JUnit FAQ page.This is done to promote the concept of test Independence, this will make sure that the tests will test their cases clearly and independently, and also ease maintainability.
This means that when you are running the tests in Eclipse, the orders are not guaranteed and keep changing. It's explained in "Can I change JUnit execution order?" on how you can possibly fix the order, even though its not a good practice.

Selecting specific tests to run in gradle

I'm trying to fix our messy failing test runs, and, unfortunately, I'm very new to gradle. We currently have testng, junit, and I'd like to add some spock tests to the mix as well. I'm not quite sure how gradle determines which tests to run when I type "gradle test". How can I prevent the testng &/or junit tests from running? How can I get gradle to start running my spock tests?
By default, the test task runs all JUnit tests it can find, which includes any Spock tests. To make it run TestNG tests instead, configure the task as follows:
test {
useTestNG()
}
If you have both JUnit and TestNG tests, you need two test tasks, one for each test framework.
To run a subset of tests, use the -Dtest.single system property. For more information, see the corresponding section in the Gradle User Guide.
You may provide using the command line:
$> gradle test --tests org.somewhere.MyTestClass
Or even
$> gradle test --tests org.somewhere.MyTestClass.my_test_case
$> gradle test -Dtest.single=YourTestClass

How to set up a Hudson server to run cppunit tests

I'm having problems setting up my Hudson server to run cpp unit tests so I can output an .xml file. I tried searching the web for some more straight forward instructions on how to set this up but still don't understand how to. It sounds like I need to set up ant to run...but how??
I'm currently running Hudson ver 1.352.
Any suggestions will be greatly appreciated.
Kat
I'm assuming you have some existing tests implemented in CppUnit that you can run by themselves. You could get these running in your Hudson job by using Ant, but since I don't think there is a CppUnit task for Ant you'd have to do it with an exec task.
It might be just as easy to call a shell script from Hudson to run your tests; you should then be able to get it to display the test results by checking the "Display JUnit test results" post-build action in your Hudson job and specifying the path to your results XML file (it's been a while, but as I remember CppUnit tests are in the same XML format as JUnit).
Let me know if I'm making any wrong assumptions.

Execute command in Hudson as Post-build Actions

I am new in Hudson.
I would like to execute a 'sourcecodeanalyzer' command in Hudson as Post-build Actions to generate an html report. Please let me know is this at all possible, if yes let me know the Hudson configuration steps to execute the command.
Your earliest response in this regard will be extremely helpful.
Thanks in advance.
Yes, it is almost certainly possible.
You will need to configure the Hudson project to have either a post-build action or a build step that runs your source code analyzer.
You've not stated in your question precisely which analyzer - it may be that Hudson already has a plug-in installed for it, in which case it may be listed on the Config page for the project at the bottom under Post-build Actions.
If not, next check to see if there's a plug-in available for the analyzer that hasn't been installed. From the main Hudson page select Manage Hudson, then Manage Plugins, and choose the Available tab. If there is a plug-in available it's definitely a good idea to use it as they are generally very well integrated with Hudson itself.
As a last resort you'll have to configure a build step to run the analyzer. Configure the project, then choose "Add build step". The drop-down that appears depends on your environment (Windows or Linux) but should include the ability to run a shell command or batch file. You can configure your analyzer there.
(If you're building Windows Visual Studio applications, a more flexible way that I've used is to use the MSBuild plug-in for builds, and have an MSBuild script that builds the application and then runs analysis tools. This can automate pretty much everything: mine builds the application, builds an acceptance test database, runs the acceptance tests and copies the result HTML to a page linked from the project.)
You could create a new job with a "Execute Shell" build step. Type in the command you wish to run in the text box. Then all you have to do is trigger this job by selecting:
"Build after other projects are built"
And select the trigger job from the list.
Hope this helps!
As a follow up to Jeremy's post. If you don't see the ability to add post build steps, you might work with maven jobs. In that case you need the Hudson M2 Extra Steps Plugin. This will give you pre and post build steps.
I use the 'Post build task' plugin to delete some resources after a build. You could call any shell script or command lines. If you want you could make the call depends on some logging output.
there is one best way to solve this:
Upgrade to Fortify SCA 2.6.x (as of writing, latest version is 2.6.5).
Download the Fortify Maven Plugin version 2.6 from https://customerportal.fortify.com and install it into your Hudson server's Maven repository.
Update your project's pom to carry out the Fortify scan. There is an example provided with the Maven plugin.
Currently , I am experiment with sonar plug in. It looks great check the details here
http://sonar.codehaus.org/a-new-hudson-plugin-for-a-closer-integration-with-sonar/
http://wiki.hudson-ci.org/display/HUDSON/Sonar+plugin