Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only run unit tests which's respective source code has changed?

I am running unit tests and Selenium tests in our Jenkins CI server. As we all know, tests take long to run in a large project.

Is there a tool/framework for Java which could only trigger tests whose respective source code has changed? This because not every commit to SCM affects all areas of the source code...

I am using Cobertura for code coverage and Surefire for reporting.

EDIT: I found Atlassian Clover, but I am searching for a free solution.

like image 379
user1340582 Avatar asked Nov 15 '12 08:11

user1340582


People also ask

Which one is used to run the unit tests?

Unit tests can be performed manually or automated. Those employing a manual method may have an instinctual document made detailing each step in the process; however, automated testing is the more common method to unit tests. Automated approaches commonly use a testing framework to develop test cases.

What errors are commonly found during unit testing?

Unit testing considerations What errors are commonly found during Unit Testing? (1) Misunderstood or incorrect arithmetic precedence, (2) Mixed mode operations, (3) Incorrect initialization, (4) Precision inaccuracy, (5) Incorrect symbolic representation of an expression.

Which of the following is correct about a unit test case?

Q 10 - Which of the following is correct about a Unit Test Case? A - A Unit Test Case is a part of code which ensures that the another part of code (method) works as expected.

What is the recommended way to separate the code that you are testing from its related dependencies?

Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.


2 Answers

I am running unit tests and Selenium tests in our Jenkins CI server. As we all know, tests take long to run in a large project.

This is the problem I'd tackle. Split your project into multiple logical units (e.g. Persistence Layer, Service Layer, Web Layer) and test them individually. That way you only need to run Selenium tests when the web layer has changed and the build time per artifact grows shorter

like image 113
Sean Patrick Floyd Avatar answered Sep 29 '22 20:09

Sean Patrick Floyd


In general you cannot reliably infer what code is covered by what test without running it, and even when you do run your tests the execution path could be different from run to run. I suspect perfect analysis like this is equivalent to Halting Problem.

However you can infer what tests are likely to exercise changed code using either coverage from previous test run or imports in the test case files.

In the end, you do want to run your entire test suite, however, you are right that running tests over changed code is more important because 1. developer knows they messed up earlier and 2. if you experience a lot of changes, it makes sense to test newest revision first and go to earlier revisions only to bisect problems.

Google famously claimed * that their hacked code repository runs unit tests at commit time ** and that they tweaked the test framework to determine what tests to run first. I don't know if their changes are public.

* video talk to conference presentation, I don't recall exact name or link

** strictly after commit, followed by automatic rollback if tests begin to fail

like image 35
Dima Tisnek Avatar answered Sep 29 '22 22:09

Dima Tisnek