Running unit tests with Jenkins and Ant - java

I have just started using Jenkins for a small project and as part of that, would like the unit tests to be run every time it builds.
I understand that you need to put a test target in your build file and specify this target in the job configuration, but I am using the build file generated by NetBeans, therefore if I modify it I assume it will just get overwritten.
My project is an enterprise application which is made of a single jar. The build script I am using is for the enterprise application, not the jar, so that an ear file is produced which I can then deploy to an application server.
Currently the build passes and produces and ear file which I can deploy and works, but nowhere in the console output does it show that it is running the tests.
The unit tests are in the jar project, so how can I get Jenkins to run the unit tests when it builds the ear file?

Question can be bit more clear - When your tests are executed ?
If your build runs the unit tests but not shown in the console, you should find a way to display it in console.
Like in Unix, executable run_qa_test > capture_results.txt - will capture all the results in the txt file and not display in console.
This can be modified as run_qa_test | tee capture_results.txt - This will display the output in console and record in txt file as well.
Or ,if you build produces ear file and if your tests in jar is not executed as part of the build job, you can create a new job as downstream of the build job and run those tests finally.
Or if needed to run only as part of the same build job you need to modify the build process

Related

Running JUnit tests together with Java project

I'm writing code in Intellij and have a JUnit test class included in a project and i understand that running of JUnit should always be done at build time.
Is there a way to first run the JUnit and only if there were no test error run the project itself ? I want them to run together with 1 click (NOT run them seperately/manually).
Also, i would like the above to work even when the project is packed as a .jar file.
How can it be done ?
Thanks !
In Intellij:
Run -> Edit Configurations
Create a JUnit configuration for your tests
Create a Run configuration for your project.
And on "Before Launch": Add -> Run Another Configuration and choose the one created at point 1.
It doesn't matter how your project is packed (jar, ...)
Normally this is done by using a build management tool like: maven, gradle, ant. In this way the build tool will run tests for you and stop if they fail.
With maven, it's just a command: mvn clean package exec:java which will compile code, build project, run tests and execute your code.
See example project: https://github.com/HaveACupOfJava/maven-jar-demo

Maven separate Building and packaging and Cucumber Integration tests

Currently I am working in a project that integrates Gitlab + Jenkins + Maven.
This is a Maven Java project and we have UT and Integration tests.
I designed a pipeline for the CI that looks like this:
Build Core Package and Run UT
Build WebPage and Run UT
Run Integration Tests written in Cucumber.
Deploy to a staging Server
On paper this looks good but now I am trying to implement this and I am having some issues.
Is there a simple way to save the java packages in per Git Branch? Each branch will compile and create a Jar file in step 1 that will be needed in Step 2.
In step 3 how can I use the war built in step 2 to run the tests? Currently I have all inside the lifecycle of Maven, but cannot find a way to split this.
Thanks
You can use jenkins to perform this job
I don't think you want to be putting build assets into source control. You should be able to use Jenkins to run the individual steps of your maven build and use the intermediate jar and war files that it creates in its working directory. If you can't split them out into separate steps within maven for some reason, you may need to simply do the whole maven build and then run the tests with the working files which it shouldn't remove.

using command line how run junit test present inside the war file

using maven we create a war file and we need to be deployed this war in tomcat(application server) for different environment (DEV/QA/UAT).
We need to run the JUNIT test for each of the environment before deploying.
we have written nearly 60 junit test which need to passed
Is is possible to run the junit test for war file?
if yes how to run all junit test sitting inside the war file
WAR file is deployment artifact that shouldn't include any unit tests. As #Gerold Broser correctly pointed out, Maven handles excluding of unit tests for you if you place them into "src/test/java". Don't ever try to put them into "src/main/java".
we have written nearly 60 junit test which need to passed
Is is possible to run the junit test for war file?
If they are true unit tests, they don't need to be executed against deployed war service. They are testing small chunks of functionality and are generally executed during test phase of maven lifecycle.
If tests are firing requests against deployed server, at that stage they are not unit tests anymore and should be probably placed into separate test project.
Proper place where to run your tests is always Continuous Integration server, so one way or the other make sure that execution is automated! Tests without CI server are waste of time.
BTW
There is maven-tomcat7-plugin to start tomcat and deploy it during interation-test phase of Maven lifecycle, but this maven plugin doesn't seem to be maintained anymore and doesn't work with Servlet 3.0 Java configs and Tomcat 8. So I don't recommend that path.

How to run selenium test cases in jenkins?

I had been introduced to concept of CI lately and was trying to work on jenkins CI. I was stuck up in one thing . How to trigger executable testng files in jenkins CI. For ex locally in our machines we just run testng.xml to execute couple of test cases. In the same way how can we trigger this xml file to run in jenkins CI ?
In most cases with jenkins you wouldn't use an executable. Normally you'd run the wrapper for the tests (Junit/Nunit etc.) which Jenkins is fully capable of running on it's own.
You can use this article to run TestNG tests using Maven:
Running TestNG tests using maven
After configuration is completed just add Invoke top-level Maven targets step to the Build Steps in Jenkins (Maven plugin should be installed). The target should be test in this case.
If you will face with any errors during configuration, try to google them.
If you are not using any build tool like maven or ant, you can invoke it from command line as we'll and specify your suite file. Make sure to set the correct class paths http://testng.org/doc/documentation-main.html#running-testng
You can put this as a build step in Jenkins.
Add a compilation step prior to this step. I haven't ever tried it - have always used ant or maven, but that is where I would start exploring.

Eclipse + Selenium + TestNG - Continuous Integration - **No** Ant, Maven, Etc

I'm looking at setting up a continuous integration server for running selenium and testng unit tests.
I know you can do so by using maven or ant, but I don't want to add a 2nd build system into our project. I've read all about it, another team here is using maven, but I'm trying as hard as I can to avoid it. (Would appreciate not turning this into an Ant or Maven debate.)
From what I've read, if I was using Intellij their TeamCity product could do this no problem from the project config files - but we're not (unfortunately, I'd love to), we're using Eclipse. If we were using Netbeans, apparently everything is already stored as an Ant project so that wouldn't be a problem.
Curious if anyone has had any success setting up a continuous integration server with an Eclipse project, without adding in another build tool to the project. We can do this without a problem on a developers local machine, seems like it shouldn't be to hard to do for an integration server...
Edit: I found another stack overflow topic on how to run the eclipse build from the command line -
Build Eclipse Java Project from Command Line
"eclipsec.exe -noSplash -data "D:\Source\MyProject\workspace" -application org.eclipse.jdt.apt.core.aptBuild"
I tried it out, and manually copying over the results into Tomcat definitely works (though you end up copying .class files rather than a .jar file). Most of the continuous integration products say they can use any command line tool to do the build.
Still wondering if anyone has done it before though...

Categories

Resources