steps:
1. Create an android project.
2. Create MathUtils.java(sample class, we will write test cases for this class) and write functions to add, subtract, multiply, divide two numbers.
MathUtils.java should be something like
3. Introduction to local junit test cases in Android.
Local junit tests are tests that run on your local machine, with out needing access to the Android framework or an Android device. If your unit test has no dependencies or only has simple dependencies on Android you should run your test on a local development machine. This testing approach is efficient because it helps you avoid the overhead of loading the target app and unit test code onto a physical device or emulator every time your test is run. Consequently, the execution time for running unit test is greatly reduced. For more information please read this.
4. Write test cases for the MathUtils methods with junit and data providers.
Open MathUtils class and follow the steps
a. Right click -> Go To -> Test
b. Click Create New Test ...
c. select all the methods ( add(a:int, b:int):int, subtract(a:int, b:int):int, multiply(a:int,
b:int):int, divide(a:int, b:int):int )
d. click ok
e. Choose Destination Directory with is under ../app/src/test/java/com/...
f. click ok
g. Now navigate to app/src/test/java/.../MathUtilsTest
you should be able to see all the test methods now. It should be something like
In order to add junit dataprovider, add the following dependency in build.gradle file.
Create MathUtilsDataProvider class inside app/src/test/java/…./
Create testAddData() method (name can be anything) to return data to the test cases with @DataProvider annotation
in the data[] array data[0], data[1] are input.
in the return object array first element is the input and the second is expected output.
Similarly add create data provider for subtraction, multiplication, division.
Now MathUtilsDataProvider should look something like this.
How the Test cases take the data from data provider ?
Add @RunWith(DataProviderRunner.class) annotation above class - This allows to run the test method with parameter.
Add the following annotation after @Test annotation on every method of MathUtilsTest
here testAddData is the DataProvider’s method name.
We need MathUtils object to test the add, subtract, multiply and divide methods. Create a @Before method to setup mathUtils object as follows.
Now modify the add Test as follows.
Similarly change all the other methods.
Now your MathUtilsTest should look something like this.
Run the test cases locally by clicking the arrow button on each method or on class name or by right clicking the MathUtilsTest -> RunMathUtilsTest.
after running this we should be able to see 12 tests done: 4 failed message.
If you want to see test coverage use runtestwithcoverage option.
5. Push the code to github.
https://github.com/Franklin2412/JenkinsTestApp.git
6. Setting up jenkins server locally.
I use homebrew(http://brew.sh/) to install Jenkins.
This install jenkins locally. (/usr/local/opt/jenkins)
if you want to run jenkins on every login you can use
Then run
At this point you should be able to see jenkins home at
if you want Configure ip or port open
Install Required plugins.
Mangage Jenkins - > Mangage Plugins -> Available Tabs
-
Email Extension Plugin
-
Git plugin
-
Gradle plugin
-
HTML Publisher plugin
Job Configuration Steps.
1. To create a new job -> click _New Item_.
2. Enter name (This is your jenkins job name)
3. Select Freestyle project and click OK
4. Under Source Code Management
select Git
Fill Repository URL : https://github.com/Franklin2412/JenkinsTestApp.git
5. Under Build
click Add build step -> Invoke Gradle script
Select Use Gradle Wrapper
6. Click Add build step -> Execute Shell
Add the following command
./gradlew test
7. Under Post-build Actions
Click Add post-build action -> Publish HTML report
Click Add
Give following paths
HTML directory to archive : app/build/reports/tests/debug
Index page[s]: index.html
TEST-com.laminin.franklinmichael.junitjenkinstestapp.MathUtilsTest.xml
8. Click apply and save to save the configurations.
8. Run the test cases using jenkins.
Click build now button.
9. Generate and display test results.
Once build is done you can see the HTML Report option inside the job
at (http://localhost:5500/job/GitHubJunit/)
10. Build on every push.
To build this job on every push
Click Poll SCM option in Build Trigger option and set the schedule values as * * * * *
This will keep listening github repo for any new push. if there is a new push it will automatically build the job.
11. Send email notification.
Click Add post-build action dropdown from Post Build Action section and select E-mail Notification
Fill all the email address space separated.
Click apply and save
Click Manage jenkins then Configure
Go to Extended E-mail Notification
Click advance and fill the values.
SMTP server -> smtp.gmail.com
Default user E-mail suffix -> @gmail.com
User Name -> franklin2412@gmail.in
Password -> **********
SMTP port -> 557
Charset -> UTF-8
Build the job again. If any test case fails it will generate report and send it to mails.