Unit Testing Continuous Integration PYUNIT AND JENKINS FRAMEWORK Presenter Rachita Agasthy
Unit Testing Goal – Isolate parts of the code, test their individual working. Why do Unit testing? Unit testing is a part of most of software development methodologies in use today Agile Methodologies Extreme Programming Test Driven Development
Advantages and Disadvantages Advantages Reduction in flaws – Statistics suggest 90% reduction in bugs during QA process Repeatability – Can reuse the same tests as and when you make changes Disadvantages Requires approximately 30% more time at the start of the project Written by developer, not possible to cover all the cases Frameworks Available – GUnit(C++), JUnit(Java), PyUnit(Python), test-unit(Ruby) and so on.
Unit testing example - PyUnit class Calculator(object): def add(self, value1, value2): return value1 + value2 Actual Code:Test Code: class CalculatorUnitTest(unittest.TestCase): def setup(self): // Code that is common to every // test in this class. myCalculator = Calculator() def test_add(self): // Positive value addition result = myCalculator.add(100, 20) errMsg = “Expected 120 but got ”+ result assert result == 120, errMsg // Negative value addition result = myCalculator.add(-10, -20) errMsg = “Expected -30 but got ”+ result assert result == -30, errMsg if __name__ == "__main__": unittest.main()
Mocking in Unit Tests class Calculator(object): def add(self, value1, value2): return value1 + value2 def multiply(self, value1, value2): result_arr = [] for i in xrange(0,value2/2) result.append(add(value1, value1) if value2 % 2 != 0: result = value1 for value in result_arr: result = result + value1; return result Actual Code:Test Code: class CalculatorUnitTest(unittest.TestCase): def setup(self): // Code that is common to every // test in this class. myCalculator = Calculator() def test_multiply(self): val1 = 10 val2 = 20 // Positive value multiplication myCalculator.add = MagicMock(return_value = 20) result = myCalculator.multiply(val1, val2) errMsg = “Expected 200 but got ”+ result assert result == 200, errMsg if __name__ == "__main__": unittest.main()
Continuous Integration Development work is integrated at a predefined time or event Resulting work is automatically tested and built Advantage ◦Automated Unit Testing ◦Development errors are identified very early in the process ◦Continuous Quality Control Usage: Extensively used in Extreme Programming Frameworks Available – CruiseControl, Jenkins, Buildbot and so on.
Jenkins Open source Java based tool. Basic functionality ◦Detect code changes in the code repository ◦Build different parts of the code ◦Run unit tests ◦Run the different components of the system ◦Verify the output ◦In case of failures, notify the developer who caused the failure. ◦Also notify other developers Requirements ◦Requires installation on the server ◦Accessible through a web page
Homepage
Creating new Job
Enter Job Details
Advanced Job Options
When to run the job?
What happens when job runs?
Other features ◦Managing Jenkins
More managing options
Thank you!