Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture # 19
Ayesha Kanwal
SEECS- NUST
2 Unit Testing
First level of testing.
Refers to testing program units in
isolation.
A program unit implements a
function, it is natural to test the unit
before it is integrated with other units.
What is a Unit?
3
Syntactically, a program unit is a piece of code,
such as a function or method or class, that is
invoked from outside the unit and that can invoke
other program units.
Moreover, a program unit is assumed to
implement a well-defined function providing a
certain level of abstraction to the implementation
of higher level functions.
Why units need to be tested?
The function performed by a program unit may
not have a direct association with a system-
level function. Thus, a program unit may be
viewed as a piece of code implementing a
“low”-level function
Given that a program unit implements a
function, it is only natural to test the unit
before it is integrated with other units.
Unit Testing in Stand-alone
manner
4
testing.
Note: Static unit testing is not an
alternative of dynamic unit testing, a
programmer performs both kinds of tests.
Dynamic Unit Testing
12
environment.
The actual execution environment is emulated by writing
recommended.
You can download the Eclipse IDE
package com;
import static org.junit.Assert.*;
import org.junit.Test;
public class MultiplyTest {
@Test
public void testMultiply() {
Junit test = new Junit();
int result = test.multiply(5, 5);
assertEquals(25, result);
}
}
Creating a test suite
27
package com.emertxe;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
Parameterized unit test cases
using JUnit
34
@RunWith(Parameterized.class)
public class AddParamTest {
private int expectedResult;
private int firstVal;
private int secondVal;
Calculator cal;
@Before
public void init() {
cal = new Calculator();
}
Parameterized unit test cases
using JUnit
35
@Test
public void testAdd() {
Assert.assertEquals(expectedResult,
cal.add(firstVal, secondVal));
}
}
Summary
37