Introduction To UnitTesting & JUnit
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Introduction To UnitTesting & JUnit

on

  • 545 views

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.

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.

Statistics

Views

Total Views
545
Views on SlideShare
544
Embed Views
1

Actions

Likes
1
Downloads
21
Comments
0

1 Embed 1

http://www.slideee.com 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Introduction To UnitTesting & JUnit Presentation Transcript

  • 1. Introduction to UNIT TESTING & JUNIT By: DHEERAJ SHANDILYA LinkedIn Id: http://in.linkedin.com/pub/dheeraj-shandilya/3a/77/5a2 Mindfire Solutions, India
  • 2. U n i t T e s t i n g 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
  • 3. U n i t T e s t i n g 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.
  • 4. U n i t T e s t i n g 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.
  • 5. U n i t T e s t i n g What is Unit Testing? Static Analysis Static unit testing offers the minimum path coverage It helps in checking structural properties of code Here we check syntax, unreachable codes, undeclared/uninitialized variable, parameter type mismatch... Dynamic Analysis Dynamic unit testing detects control and data flow problems It helps in checking the behavior of unit source code on test data Here we will check classes, methods, boundary condition, logic errors...
  • 6. U n i t T e s t i n g 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?
  • 7. U n i t T e s t i n g 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?
  • 8. U n i t T e s t i n g Why to do Unit Testing? Easy to detect defect and debug
  • 9. U n i t T e s t i n g 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
  • 10. U n i t T e s t i n g 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.
  • 11. U n i t T e s t i n g 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 here http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks Tools & Framework
  • 12. 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.
  • 13. History of Junit 1994 Sunit came into existence 1997 Initiation of Junit 2000 Junit Officially Released 2002 Eclipse came into existence
  • 14. Annotations in Junit
  • 15. @Test public 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
  • 16. @Before public 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). @After public 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
  • 17. Annotations in Junit @BeforeClass public 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. @AfterClass public 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.
  • 18. . 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
  • 19. 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
  • 20. Lets do some basic testing in Eclipse IDE
  • 21. 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
  • 22. 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 ...
  • 23. Questions ? ?
  • 24. Thank You