Professional Documents
Culture Documents
GVBM :
Thy
NGUYN
TRC
THC
V Vn Mnh
13520495
L Khnh Linh
13520449
c Khi
13520408
JUnit ???
JUnit test c th t chc thnh b test ( test suite ) cha nhiu php
th ( test case ) hoc c th cha cc test suite khc
Pass
Fail
Ch gii
Ch gii trong JUnit cung cp cho chng ta thng tin v cc phng
thc test , nh phng thc no s chy trc & sau cc phng thc
test , phng thc no hay lp no s b b qua trong qu trnh thc thi
.
@Test
Thng bo JUnit rng phng thc public void di y hot ng nh
test case
@Before
Cc phng thc km ch gii ny s c thc thi trc mi phng
thc test
@After
Ch thch rng phng thc public void km @After s khin cc
phng thc c thc thi sau phng thc Test
@BeforeClass
Ch thch phng thc public static void c thc thi trc ht bt k
phng thc test no trong class
@AfterClass
Thng dng khi thc hin hot ng dn dp , ty xa ( sau khi chy
xong tt c test
@Ignore
JUnit API
Gi quan trng nht trong JUnit l junit.framework bao hm tt c cc lp c bn
Assert
1 b cc phng thc xc thc ( assert )
Test Case
Php th nh ngha b khung chun chy nhiu
b test
TestResult
TestResult thu thp cc kt qu tr v khi thc thi 1
test case
TestSuite
TestSuite cha phc hp nhiu b test
Lp Assert
PUBLIC CLASS ASSERT EXTENDS
JAVA.LANG.OBJECT
Void fail() : Phng thc ny lm cho test hin hnh tht bi,
phng thc ny thng c s dng khi x l cc bit l
@Test
public void testAssertions() {
//test data
String str1 = new String ("abc");
String str2 = new String ("abc");
String str3 = null;
String str4 = "abc";
String str5 = "abc";
int val1 = 5;
int val2 = 6;
String[] expectedArray = {abc", def", bbb"};
String[] resultArray = {abc, def", bbb"};
//Check that two objects are equal
assertEquals(str1, str2);
//Check that a condition is true
assertTrue (val1 < val2);
//Check that a condition is false
assertFalse(val1 > val2);
//Check that an object isn't null
assertNotNull(str1);
//Check that an object is null
assertNull(str3);
//Check if two object references point to the same object
assertSame(str4,str5);
//Check if two object references not point to the same object
assertNotSame(str1,str3);
//Check whether two arrays are equal to each other.
assertArrayEquals(expectedArray, resultArray);
}
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import
org.junit.runner.notification.Failure;
public class TestRunner2 {
public static void main(String[] args)
{
Result result =
JUnitCore.runClasses(TestAssertions.class);
for (Failure failure : result.getFailures())
{
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
Lp Test Case
PUBLIC ABSTRACT CLASS TestCase extends Assert implement Test
int countTestCases()
>> m s php th c gi bi run(TestResult result)
TestResult createResult()
>> To i tng TestResult mc nh
String getName()
>> Gets the name of a TestCase.
TestResult run()
Phng thc chy test v thu thp kt qu vi i tng TestResult
void run(TestResult result)
Chy test case v xut kt qu trong TestResult
void setName(String name)
Sets the name of a TestCase.
void setUp()
VD nh thc hin m kt ni
void tearDown()
VD nh hy kt ni
String toString()
Tr v chui i din cho php th .
setUp() v tearDown()
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
public class TestJunit2 extends TestCase {
protected double fValue1;
protected double fValue2;
@Before
public void setUp() {
fValue1= 2.0;
fValue2= 3.0;
}
@Test
public void testAdd() {
//Dem so phep thu
System.out.println(So luong Test Case = "+ this.countTestCases());
//test getName
String name= this.getName();
System.out.println("Test Case Name = "+ name);
Lp TestResult
Public class TestResult extends <Object>
void addError(Test test, Throwable t)
Thm bo li trong danh sch li
void endTest(Test test)
Thng bo test hon tt
int errorCount()
Tr v s li pht hin c
int failureCount()
Tr v s kt qu sai pht hin c
void run(TestCase test)
Chy TestCase.
int int runCount()
Tr v s lng test ang chy
void startTest(Test test)
Thng bo test chun b chy
void stop()
nh du vic chy test nn ngng .
import org.junit.Test;
import junit.framework.AssertionFailedError;
import junit.framework.TestResult;
public class TestJunit3 extends TestResult {
// add the error
public synchronized void addError(Test test, Throwable t) {
super.addError((junit.framework.Test) test, t);
}
// add the failure
public synchronized void addFailure(Test test, AssertionFailedError t) {
super.addFailure((junit.framework.Test) test, t);
}
@Test
public void testAdd() {
// add any test
}
// Marks that the test run should stop.
public synchronized void stop() {
//stop the test here
}
}
To TestRunner chy Test case ( vd : TestRunner3 )
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner3 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit3.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
Test Suite
package Money;
import static org.junit.Assert.*;
import org.junit.Test;
public class MoneyTest {
@Test
public void testEqualsMoney() {
Money m1 = new Money(200, VND);
Money m2 = new Money(1000, VND);
assertTrue(m1.equals(new Money(200, VND)));
assertFalse(m1.equals(m2));
}
@Test
public void testAddMoney() {
Money m1 = new Money(200, VND);
Money m2 = new Money(1000, VND);
Money result = m1.AddMoney(m1,m2);
Money expected = new Money(1200, VND);
assertTrue(Result,expected.equals(result));
}
}
Test lp
import junit.framework.*;
public class TestGame extends TestCase {
private Game game;
private Ship fighter;
public TestGame(String name) {
super(name);
}
Lp li test
a) Cactus
Cactus l mt framework unit testing ngun m dng test
cho cc on m pha bn server ca Java. c bit l Cactus
cho php bn test Servlet, JSP, v Servlet filter. Cactus tha k
t JUnit cung cp 3 lp con ca lp junit.framework.TestCase
l cc lp:
org.apache.cactus.ServletTestCase
org.apache.cactus.JspTestCase
org.apache.cactus.FilterTestCase
Mi test case ca Cactus cung cp 1 chc nng c bit.
Cactus test thc thi trn c client v server. Khi s dng Cactus
bn ch cn to mt lp tha k t 1 trong 3 lp trn. Sau
Cactus s to ra v chy 2 th hin ca test case. Mt chy
trn JVM pha client, ci cn li chy bn trong JVM ca mi
trng chy servlet (servlet container) pha server. Bn client
pha client cho php HTTP headers v cc tham s HTTP c
thm vo cc yu cu i ra. Bn pha server gi thc thi cc
phng thc bn trong servlet ca bn thc hin bt k xc
nhn no, v sau s gi phn hi ngc tr li cho pha
client. Tp n bn pha client xc nhn phn hi t bn server
b) HttpUnit
HttpUnit l mt th vin ngun m ca Java c dng
tng tc vi cc server HTTP. Vi HttpUnit, chng trnh
Java ca bn c th truy xut trc tip n server m khng
cn thit phi s dng n trnh duyt.
HttpUnit cung cp cc API phn tch HTML, nhn thng
tin ca biu mu trang web, theo di cc siu lin kt, thit
lp cookie v thc hin cc tc v khc c lin quan n
trnh duyt web.
Ngoi ra n cng gm c mt th vin thao tc trc tip
n servlet, i khi khng cn thit phi khi ng web
server
Thng thng chng ta s dng kt hp HttpUnit v JUnit
vit cc tt. JUnit nh ngha cc framework dng
kim tra, v cc phng thc testXXX() ca bn s s dng
cc hm API ca th vin HttpUnit truy cp v kim tra
trang web
c) NUnit
NUnit l mt framework dnh cho vic testing unit trong tt
c cc ngn ng .NET. Khi u n cng c bt u t
DEMO
CM N
THY V
CC BN
THEO DI !!