introduction to unittesting & junit
DESCRIPTION
This presentation focuses on Unit Testing. If one is interested to know what, why and how to do 'Unit Testing', then this would be helpful.TRANSCRIPT
Introduction to
UNIT TESTING & JUNITBy:DHEERAJ SHANDILYALinkedIn Id: http://in.linkedin.com/pub/dheeraj-shandilya/3a/77/5a2Mindfire Solutions, India
Unit
Testing
Introduction to Unit Testing
What, why and how to do Unit Testing
Framework that will help
Introduction & History of Junit
Reference & Questions
We are going to discuss
Lets play with basic codes
Annotations and assertions in Junit
Unit
Testing
What is Unit Testing?
A Unit is the smallest possible part of application/system under test.
Testing is a process of checking/evaluating an attribute/characteristics/capability of a program/object/method/application/..... and determine that actual behavior meets with expected behavior.
Unit Testing is a process of writing test for specific part of code/system.
Unit testing help us reduce the number of bugs, time spent on debugging, create a stable and reliable application.
Unit
Testing
What is Unit Testing?
A Unit is the smallest possible part of application/system under test.
Testing is a process of checking/evaluating an attribute/characteristics/capability of a program/object/method/application/..... and determine that actual behavior meets with expected behavior.
Unit Testing is a process of writing test for specific part of code/system.
Unit testing help us reduce the number of bugs, time spent on debugging, create a stable and reliable application.
Unit
Testing
What is Unit Testing?
Static AnalysisStatic unit testing offers the minimum path coverageIt helps in checking structural properties of code
Here we check syntax, unreachable codes, undeclared/uninitialized variable, parameter type mismatch...
Dynamic AnalysisDynamic unit testing detects control and data flow problemsIt helps in checking the behavior of unit source code on test data
Here we will check classes, methods, boundary condition, logic errors...
Unit
Testing
1. If a unit is not working, it affects performance of complete system
For example Consider any system say bike, car, mobile, human being anything. Suppose mobile screen (lcd/led) is not working then you will not be able to see menu over screen. Hence you can not work/operate.
Why to do Unit Testing?
Unit
Testing
2.There is strength in unity but not in unit. It means with unit testing we can detect defect and fix bugs easily.
For example Police interrogate suspects individually to get some more clues.
If different component/unit of a system works fine, then there are More possibility that system (combining all component) will also work fine
Why to do Unit Testing?
Unit
Testing
Why to do Unit Testing?
Easy to detect defect and debug
Unit
Testing
Benefits of Unit Testing?
> It Saves time on debugging. Hence effort and money too.> It prepares the base for further testing like integration, functional Testing...> It enhances performance and provide obstacle free control.> It ensures repeatability of desired behavior> Unit Testing makes it easier to change and refactor code.> Unit Testing inspires confidence. > Unit Testing reduces the level of bugs in production code.> We will get instant feedback> It brings modularity in design
Unit
Testing
How to do Unit Testing
We have to write tests to check that the functionality of a unit, often a class(or function/methods in the class) meet its requirement.
Tests are written as the unit is developed. So this testing is done by software engineer while development
The idea is that every time you make a change to the unit you rerun the tests and see if your change has broken anything.
There are many tools and framework that will help us in unit testing.
Unit
Testing
For different technology we can use different unit testing framework. For example
Java: JUnit, TestNG...C: Ctest, Cunit, Opmock ...Python: Unittest, Py.test, Nose...Visual Basic(VB6.0): vbunit, vbaunit, TinyUnit...PHP: PHPunit, SnapTest ...
We can see complete list herehttp://en.wikipedia.org/wiki/List_of_unit_testing_frameworks
Tools & Framework
Introduction to Junit
JUnit: It is a framework for performing unit testing of Java programs. It is used to write repeatable tests. For this we need Eclipse IDE. .
Eclipse contains a base workspace and an extensible plug-in system for customizing the environment. It is used to develop application in various languages...
JUnit is linked as a JAR at compile-time.The framework resides under package junit.framework for JUnit 3.8 and earlier, and under package org.junit for JUnit 4 and later.
History of Junit
1994 Sunit came into existence
1997 Initiation of Junit
2000Junit Officially Released
2002Eclipse came into existence
Annotations in Junit
@Testpublic void method()
The @Test annotation identifies a method as a test method.
@Test (expected = Exception.class) Fails, if the method does not throw the named exception.
@Test (timeout=time in millisec) Fails, if the method/test timed out after specified time
@Ignore Ignores the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included.
Annotations in Junit
@Beforepublic void method() This method is executed before each test. It is used to prepare the test environment (e.g. read input data, initialize the class).
@Afterpublic void method() This method is executed after each test. It is used to cleanup the test environment (e.g. delete temporary data, restore defaults). It can also save memory by cleaning up expensive memory structures.
Annotations in Junit
Annotations in Junit
@BeforeClasspublic static void method() This method is executed once, before the start of all tests. It is used to perform time intensive activities, for example to connect to a database. Methods annotated with this annotation need to be defined as static to work with JUnit.
@AfterClasspublic static void method()
This method is executed once, after all tests have been finished. It is used to perform clean-up activities, for example to disconnect from a database. Methods annotated with this annotation need to be defined as static to work with JUnit.
.
There are several Juint Assertion methods . These are static methods defined in org.junit.Assert class
An assertion is a statement of expected outcome. In other words, it is the expected result of your test.
Assert Method
Assert Method
Most common of them are > Fail() - Fails test> assertNotNull(0) /assertNull(0) - assert if object is (not) null> assertFalse(b) /assertTrue(b) - asserts if b is false /true> assertEquals(a, b) - Compares two objects using equals() method> assertArrayEquals(a, b) - compares two arrays
Lets do some basic testing in Eclipse IDE
Best Practices In Unit Testing
Isolate test from other environment, classes, tests.
Self Descriptive > Name of variable, classes, methods......
Avoid conditional logics(if else...), nested loops
Use various assertion method provided by framework.> Assertion message should tell the problem. One can include business logics,
Separate the test based on type and business module
http://junit.sourceforge.net/javadoc/org/junit/Assert.html
Http://www.pluralsight.com
Http://www.youtube.com
Http://en.wikipedia.org/
Http://junit.org
Http://www.google.co.in
Credit goes to ...
Questions
? ?
Thank You