7aedf73864b8b11773cd68a04a46a390.ppt
- Количество слайдов: 36
Lesson 7 Unit Testing /JUnit/ AUBG ICo. SCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad
Lesson Contents: § Unit Testing § Introduction to JUnit § JUnit within/under Net. Beans 3
Unit Testing 4
Acceptance testing User needs Requirement specification System testing Design Integration testing Code Unit testing Categories of Testing 5
Unit Testing § § § Different modules tested separately Focus: to detect defects injected during coding UT is closely associated with coding Frequently the developer himself does UT SDM coding phase sometimes called “coding and unit testing” phase Testing 6
What is Unit Testing? § A Unit Test is a procedure to validate a single Functionality of an application. One Functionality One Unit Test § Unit Tests are automated and self-checked. § They run in isolation of each other. § They do NOT depend on or connect to external resources, like DB, Network etc. § They can run in any order and even parallel to each other. Impetus Confidential 7
JUnit – An Introduction - 8
JUnit – An introduction § JUnit is a unit testing framework for Java. § It comes from the family of unit testing frameworks, collectively called x. Unit, where ‘x’ stands for the programming language, e. g. NUnit for C#, CPPUnit, JSUnit, PHPUnit, Py. Unit, RUnit etc. Kent Beck & Erich Gamma originally wrote this framework for ‘smalltalk’, called SUnit. Impetus Confidential 9
Why choose JUnit? § JUnit is simple and elegant. § JUnit checks its own results and provide immediate customized feedback. § JUnit is hierarchial. § JUnit has the widest IDE support, Eclipse, Net. Beans, Blue. J, …. § JUnit is recognized by popular build tools like, ant, maven etc. § And… it’s free. Impetus Confidential 10
Design of JUnit § junit. framework. Test. Case is the abstract command class which you subclass into your Test Classes. § junit. framework. Test. Suite is a composite of other tests, either Test. Case or Test. Suite. This behavior helps you create hierarchal tests with depth control. Impetus Confidential 11
Design of JUnit Image Courtesy: JUnit: A Cook’s Tour Impetus Confidential 12
Write a test case § Define a subclass XXXTest of junit. framework. Test. Case public class Calculator. Test extends Test. Case { … } § Define or more test. XXX() methods that can perform the tests and assert expected results. public void test. Addition(){ Calculator calc = new Calculator(); int expected = 25; int result = calc. add(10, 15); assert. Equals(result, expected); // asserting } Impetus Confidential 13
Write a test case § Each test method has to be structured like this: public void test. Addition(){ // declarations Calculator calc = new Calculator(); // preparing input /expected/ data int expected = 25; // computing result /real, actual/ data int result = calc. add(10, 15); // comparing result data to expected data assert. Equals(result, expected); // asserting } Impetus Confidential 14
Write a test case § Override set. Up() method to perform initialization. @Override protected void set. Up() { // Initialize anything, like calc = new Calculator(); } § Override tear. Down() method to clean up. @Override protected void tear. Down() { // Clean up, like calc = null; // gc, no need of delete operator } Impetus Confidential 15
Asserting expectations § assert. Equals (expected, actual) § assert. Equals (message, expected, actual) § assert. Equals (expected, actual, delta) § assert. Equals (message, expected, actual, delta) § assert. False ((message)condition) § Assert(Not)Null (object) § Assert(Not)Null (message, object) § Assert(Not)Same (expected, actual) § Assert(Not)Same (message, expected, actual) § assert. True ((message), condition) Impetus Confidential 16
Failure? § JUnit uses the term failure for a test that fails expectedly. That is § An assertion was not valid or § A fail() was encountered. Impetus Confidential 17
Write a test case public class Calculator. Test extends Test. Case { // initialize protected void set. Up(). . . S public void test. Addition(). . . public void test. Subtraction(). . . public void test. Multiplication(). . . T 1 T 2 T 3 // clean up protected void tear. Down. Up(). . . D } Execution will be S Impetus Confidential , , T 1 D S T 2 D S T 3 D in any order. 18
Write a test suite Write a class with a static method suite() that creates a junit. framework. Test. Suite containing all the Tests. public class All. Tests { public static Test suite() { Test. Suite suite = new Test. Suite(); suite. add. Test. Suite(<test-1>. class); suite. add. Test. Suite(<test-2>. class); return suite; } } Impetus Confidential 19
Run your tests § You can either run Test. Case or Test. Suite instances. § A Test. Suite will automatically run all its registered Test. Case instances. § All public test. XXX() methods of a Test. Case will be executed. But there is no guarantee of the order. Impetus Confidential 20
Run your tests § JUnit comes with Test. Runners that run your tests and immediately provide you with feedbacks, errors, and status of completion. § JUnit has Textual and Graphical test runners. § Textual Runner >> java junit. textui. Test. Runner All. Tests or, junit. textui. Test. Runner. run(All. Tests. class ); § Graphical Runner >> java junit. swingui. Test. Runner All. Tests or, junit. swingui. Test. Runner. run(All. Tests. class ); § Confidential Impetus IDE like Eclipse, Net. Beans “Run as JUnit” 21
Mocks § Mocking is a way to deal with third-party dependencies inside Test. Case. § Mocks create FAKE objects to mock a contract behavior. § Mocks help make tests more unitary. Impetus Confidential 22
How to approach? § Test a little, Code a little, Test a little, Code a little … doesn’t it rhyme? ; ) § Write tests to validate functionality, not functions. § If tempted to write System. out. println() to debug something, better write a test for it. § If a bug is found, write a test to expose the bug. Impetus Confidential 23
JUnit – within/under Net. Beans - §. Impetus Confidential 24
JUnit within/under Net. Beans § Practical Manual: § Practical conventions § How to build unit tests? § How to run unit tests? § Demo task § Practical exercise task 25
Practical conventions § Each production code class X has a test code class Xtest derived from Test. Case class § Each method y(){…} from class X has a method testy(){…} from Xtest class § Each test code class Xtest has set. Up() method and tear. Down() method 26
Practical conventions § All production code located in a separate project folder Source Packages § All test code located in another separate project folder Test Packages § The skeletal structure and mostly the contents of a test code class Xtest and test code methods generated automatically 27
How to build unit tests? Part 1 § Create New project § Add Java class with main() method § Add user defined class to be tested l l as part of file with Java main() class or as a separate file within the same package. § Build the project and run it as a regular Java application Run > Run Main project (F 6) 28
How to run unit tests? Part 2 a § Select Java class to be tested (from Projects…) § Click the right mouse button and invoke Tools > Create Tests § Modify the generated text if necessary § Re-Build the project and run it as a JUnit test Run > Test Project(…) (Alt+F 6) § Analyze the test results 29
How to run unit tests? OR (follow option 2 b) 30
How to run unit tests? Part 2 b § Add a new File to the project File > new File… § Select category: Unit Test § Select File Type: Test for Existing Class § You have to specify the class to be tested § You have to select JUnit version: 3 § The test class automatically generated § Modify the generated text if necessary § Re-Build the project and run it as a JUnit test Run > Test Project(…) (Alt+F 6) § Analyze the test results 31
Demo Task § Net. Beans project: SBJUnit. Test. Class. Library 32
Demo Task § Net. Beans project: SBJUnit. Distance 01 33
Practical Exercise Task § Expand project SBJUnit. Test. Class. Library l l l Develop a class Power to implement arithmetic power operator with double base and integer exponent in two versions – iterative and recursive Iterative method: double powi(double base, int n){ } Recursive method: double powr(double base, int n){ } Run the powx methods regular way Run the powx methods using Junit • Create Power. Test class • Create testpowi() method and testpowr() method • Run JUnit 34
Practical Exercise Task § Expand project SBJUnit. Distance 01 l l l Develop a class Counter to implement a counter as a general purpose programming component with a count data item and methods void inc. Count() and method void dec. Count() Run the Counter class regular way Run the Counter class methods using Junit • Create Counter. Test class • Create testinc. Count() method and testdec. Count() method • Run JUnit 35
Questions? And/Or Thank You For Your Attention! 36
7aedf73864b8b11773cd68a04a46a390.ppt