Title: Software Quality Assurance MODULE TESTING NUNIT
1Software Quality AssuranceMODULE TESTING NUNIT
JUNIT
- Seminar Oana FEIDI
- Quality Manager Continental Automotive
2NUnit 2.4 Download and Installation
- www.nunit.org -gt v 2.4.8
- to verify that the program was installed
successfully \bin\nunit.util.tests.dll sau
\bin\nunit-gui.tests.dll - first steps with NUnit
http//www.nunit.org/index.php?pquickStartr2.2.
8
3JUnit Download Installation
- www.junit.org -gt download Junit4.x.zip
- Installation steps for JUnit
- unzip the junit4.x.zip file
- add junit-4.x.jar to the CLASSPATH.
- For example set classpathclasspath
- INSTALL_DIR\junit-4.x.jar
- INSTALL_DIR
- test the installation by running
- java org.junit.runner.JUnitCore
org.junit.tests.AllTests - Notice that the tests are not contained in the
junit-4.x.jar but in the installation directory
directly. Therefore make sure that the
installation directory is on the class path - Important don't install the junit-4.x.jar into
the extension directory of your JDK installation.
If you do so the test class on the files system
will not be found.
4NUnit 2.4 Create file
- in Microsoft Visual Studio 2005/2003 create a
ClassLibrary type project - add as Reference this file \bin\nunit.framework.
dll from the NUnit 2.4 installation folder - to run your file, add the new implemented .dll
file in NUnit-GUI click Run the application
5NUnit 2.4 class template
- using NUnit.Framework
- namespace Examples
- TestFixture
- public class SimpleClassTest
- SetUp
- public void Init() //all initialisations
- Test
- public void FirstTest() //test logic goes
here - Test
- public void SecondTest() //test logic goes
here -
- TearDown
- public void Final() //close files, database
connections, etc -
-
- called once before the tests
- in each TestFixtue class are run (SetUp)
- and once after (TearDown)
6NUnit 2.4 - Example
- using NUnit.Framework
- public class SimpleClass
- int Sum (int a, int b)
- return ab
-
-
- TestFixture
- public class SimpleClassTest
- Test
- public void FirstTest()
- SimpleClass sc new SimpleClass()
- Assert.AreEqual(5, sc.Sum(2,3))
-
-
7JUnit _at_Test / NUnit Test
- _at_Test
- no need to prefix your test cases with test
- your class does not need to extend from
TestCase class. - _at_Test
- public void addition()
- assertEquals(12, simpleMath.add(7, 5))
-
-
- _at_Test
- public void subtraction()
- assertEquals(9, simpleMath.substract(12, 3))
-
- Test
- Test
- public void FirstTest()
-
- SimpleClass sc new SimpleClass()
- Assert.AreEqual(5, sc.Sum(2,3))
-
8JUnit _at_Before and _at_After
- _at_Before and _at_After
- Use _at_Before and _at_After annotations for setup
and tearDown methods respectively - These methods run before and after every test
case. - _at_Before
- public void runBeforeEveryTest()
- simpleMath new SimpleMath()
-
-
- _at_After
- public void runAfterEveryTest()
- simpleMath null
-
- Tests should never make assumptions about the
order in which they are called.
9NUnit SetUp and TearDown
- SetUp
- public void Init()
-
- source new Account()
- source.Deposit(200.00F)
- destination new Account()
- destination.Deposit(150.00F)
-
10JUnit/ NUnit Exception Handling
- Use expected parameter with _at_Test annotation
for test cases that expect exception. - Write the class name of the exception that will
be thrown. - _at_Test(expected ArithmeticException.class)
- public void divisionWithException()
- // divide by zero
- simpleMath.divide(1, 0)
-
- namespace NUnit.Tests
-
- using System
- using NUnit.Framework
- TestFixture
- public class SuccessTests
-
- Test
- ExpectedException(typeof(InvalidOperationExceptio
n)) - public void ExpectAnExceptionByType()
- / ... /
- Test
- ExpectedException("System.InvalidOperationExcepti
on") - public void ExpectAnExceptionByName()
- / ... /
-
11JUnit _at_Ignore / NUnit Ignore
- Put _at_Ignore annotation for test cases you want to
ignore - You can add a string parameter that defines the
reason of ignorance if you want - _at_Ignore("Not Ready to Run")
- _at_Test
- public void multiplication()
- assertEquals(15, simpleMath.multiply(3, 5))
-
- More examples at
- http//www.cavdar.net/2008/07/21/junit-4-in-60-sec
onds/ - http//abreslav.googlepages.com/j-junit4-a4.pdf
- namespace NUnit.Tests
-
- using System
- using NUnit.Framework
- TestFixture
- public class SuccessTests
-
- Test
- Ignore("Ignore a test")
- public void IgnoredTest()
- / ... /
-
12Parameterized Test Cases I
- import org.junit.Test
- import static org.junit.Assert.
- import junit.framework.JUnit4TestAdapter
- import org.junit.runner.RunWith
- import org.junit.runners.Parameterized
- import org.junit.runners.Parameterized.Parameters
- import java.util.
- _at_RunWith(Parameterized.class)
- public class MyClassTest
-
- _at_Parameters
- public static Collection data()
- return Arrays.asList(new Object
- 0, 0, 0, 1, 1, 0, 2, 1, 1, 3, 2, 1,
4, 3, 1, 5, 5, 0, 6, 8, -2 - )
-
-
- private int expected
Example provided by Aurora (Zaharia) Pirvu
13Parameterized Test Cases II
- public MyClassTest(int expected, int input1, int
input2) - this.expected expected
- this.input1 input1
- this.input2 input2
-
-
- _at_Test public void addTest()
- assertEquals(expected, new MyClass().add(input1,
input2)) -
-
- public static junit.framework.Test suite()
- return new JUnit4TestAdapter(MyClassTest.c
lass) -
- public class MyClass
- public int add(int a, int b)
- return ab
-
Class to be tested
14NUnit 2.4 - Useful methods
- AreEqual -gt Verifies that specified values are
equal - AreNotEqual -gtVerifies that specified values are
not equal - AreNotSame -gt Verifies that specified object
variables refer to different objects - AreSame -gt Verifies that specified object
variables refer to the same object - IsFalse -gt Verifies that a specified condition is
false - IsTrue -gt Verifies that a specified condition is
true - IsNotNull -gt Verifies that a specified object is
not a null reference - IsNull -gt Verifies that a specified object is
null
15JUnit Useful methods
- assertEquals(expected, actual)
- assertEquals(String message, expected, actual)
- assertNull(Object object),
- assertNull(String message, Object object)
- assertNotNull(Object object),
- assertNotNull(String message, Object)
- assertSame(Object expected, Object actual),
- assertSame(String message, Object expected,
Object actual) - assertTrue(boolean condition),
- assertTrue(String message, boolean condition)
- fail(),
- fail(String message)
states that the test expected.equals(actual)
returns true, or both objects are null
an object reference equals null/is not null
a stricter condition than simple equality, as it
compares the object identities using expected
actual
forces a failure
fails if the condition is false
16Exercise
- Write a short program in C or Java for the
following specification - C1 up to 500 EUR value of goods there is no
rebate - C2 From 500 up to 1000 EUR there is a 2.5
rebate - C3 Over 1000, up to 5000 EUR there is a 5.0
rebate - C4 Above 5000 EUR, a 8.5 rebate is applied
- Test your implementation using the methods from
JUnit or NUnit - Create the traceability matrix between
- Specifications Code Functions Test Cases