160 likes | 355 Views
Software Quality Assurance MODULE TESTING – NUNIT & JUNIT. Seminar: Oana FEIDI Quality Manager – Continental Automotive. NUnit 2.4 – Download and Installation. www.nunit.org -> v 2.4.8
E N D
Software Quality AssuranceMODULE TESTING – NUNIT & JUNIT Seminar: Oana FEIDI Quality Manager – Continental Automotive
NUnit 2.4 – Download and Installation • www.nunit.org -> 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?p=quickStart&r=2.2.8
JUnit: Download & Installation • www.junit.org -> download Junit4.x.zip Installation steps for JUnit: • unzip the junit4.x.zip file • addjunit-4.x.jar to the CLASSPATH. For example: set classpath=%classpath%; 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.
NUnit 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
NUnit 2.4 – class template usingNUnit.Framework; namespace Examples { [TestFixture] public class SimpleClassTest { [SetUp] public void Init() { //all initialisations; } [Test] public voidFirstTest() { //test logic goes here; } [Test] public voidSecondTest() {//test logic goes here; } … [TearDown] public void Final() { //close files, database connections, etc; } } } • test suite • called once before the tests • in each TestFixtue class are run (SetUp) • and once after (TearDown)
NUnit 2.4 - Example using NUnit.Framework; public class SimpleClass { intSum (int a, int b) { return a+b; } } [TestFixture] public class SimpleClassTest { [Test] public void FirstTest() { SimpleClass sc = new SimpleClass(); Assert.AreEqual(5, sc.Sum(2,3)); } }
JUnit: @Test / NUnit [Test] • @Test • no need to prefix your test cases with “test • your class does not need to extend from “TestCase” class. @Test public void addition() { assertEquals(12, simpleMath.add(7, 5)); } @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)); }
JUnit: @Before and @After • @Before and @After • Use @Before and @After annotations for “setup” and “tearDown” methods respectively • These methods run before and after every test case. @Before public void runBeforeEveryTest() { simpleMath = new SimpleMath(); } @After public void runAfterEveryTest() { simpleMath = null; } • Tests should never make assumptions about the order in which they are called.
NUnit: [SetUp] and [TearDown] [SetUp] public void Init() { source = new Account(); source.Deposit(200.00F); destination = new Account(); destination.Deposit(150.00F); }
JUnit/ NUnit: Exception Handling • Use “expected” parameter with @Test annotation for test cases that expect exception. • Write the class name of the exception that will be thrown. @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(InvalidOperationException))] public void ExpectAnExceptionByType() { /* ... */ } [Test] [ExpectedException("System.InvalidOperationException")] public void ExpectAnExceptionByName() { /* ... */ } } }
JUnit: @Ignore / NUnit: [Ignore] • Put @Ignore annotation for test cases you want to ignore • You can add a string parameter that defines the reason of ignorance if you want @Ignore("Not Ready to Run") @Test public void multiplication() { assertEquals(15, simpleMath.multiply(3, 5)); } More examples at: http://www.cavdar.net/2008/07/21/junit-4-in-60-seconds/ 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() { /* ... */ } } }
Parameterized 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.*; @RunWith(Parameterized.class) public class MyClassTest { @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; private int input1; private int input2; Example provided by Aurora (Zaharia) Pirvu
Parameterized Test Cases II public MyClassTest(int expected, int input1, int input2) { this.expected = expected; this.input1 = input1; this.input2 = input2; } @Test public void addTest() { assertEquals(expected, new MyClass().add(input1, input2)); } public static junit.framework.Test suite() { return new JUnit4TestAdapter(MyClassTest.class); } public class MyClass { public int add(int a, int b) { return a+b; } } Class to be tested
NUnit 2.4 - Useful methods • AreEqual -> Verifies that specified values are equal • AreNotEqual ->Verifies that specified values are not equal • AreNotSame -> Verifies that specified object variables refer to different objects • AreSame -> Verifies that specified object variables refer to the same object • IsFalse -> Verifies that a specified condition is false • IsTrue -> Verifies that a specified condition is true • IsNotNull -> Verifies that a specified object is not a null reference • IsNull -> Verifies that a specified object is null
JUnit: 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
Exercise • 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