introduction to unittesting & junit

24
Introduction to UNIT TESTING & JUNIT By: DHEERAJ SHANDILYA LinkedIn Id: http://in.linkedin.com/pub/dheeraj-shandilya/3a/77/5a2 Mindfire Solutions, India

Upload: mindfire-solutions

Post on 17-May-2015

679 views

Category:

Software


7 download

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

Page 1: Introduction To UnitTesting & JUnit

Introduction to

UNIT TESTING & JUNITBy:DHEERAJ SHANDILYALinkedIn Id: http://in.linkedin.com/pub/dheeraj-shandilya/3a/77/5a2Mindfire Solutions, India

Page 2: Introduction To UnitTesting & JUnit

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

Page 3: Introduction To UnitTesting & 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.

Page 4: Introduction To UnitTesting & 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.

Page 5: Introduction To UnitTesting & JUnit

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...

Page 6: Introduction To UnitTesting & JUnit

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?

Page 7: Introduction To UnitTesting & JUnit

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?

Page 8: Introduction To UnitTesting & JUnit

Unit

Testing

Why to do Unit Testing?

Easy to detect defect and debug

Page 9: Introduction To UnitTesting & JUnit

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

Page 10: Introduction To UnitTesting & JUnit

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.

Page 11: Introduction To UnitTesting & JUnit

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

Page 12: Introduction To UnitTesting & JUnit

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.

Page 13: Introduction To UnitTesting & JUnit

History of Junit

1994 Sunit came into existence

1997 Initiation of Junit

2000Junit Officially Released

2002Eclipse came into existence

Page 14: Introduction To UnitTesting & JUnit

Annotations in Junit

Page 15: Introduction To UnitTesting & 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

Page 16: Introduction To UnitTesting & 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

Page 17: Introduction To UnitTesting & 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.

Page 18: Introduction To UnitTesting & 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

Page 19: Introduction To UnitTesting & JUnit

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

Page 20: Introduction To UnitTesting & JUnit

Lets do some basic testing in Eclipse IDE

Page 21: Introduction To UnitTesting & JUnit

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

Page 22: Introduction To UnitTesting & JUnit

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 ...

Page 23: Introduction To UnitTesting & JUnit

Questions

? ?

Page 24: Introduction To UnitTesting & JUnit

Thank You