Professional Documents
Culture Documents
Unit Test Final
Unit Test Final
Nhm 10
Cc ni dung chnh
nh ngha Unit Test Tm quan trng Unit Test Phng php xy dng Unit Test Cc i tng thay th (MOCK Object) Cc kinh nghim trong Unit Test Cc cng c h tr
Unit Test l g?
- Unit Testing, kim tra tng b phn rt nh, tng unit ring bit trong source code ca chng trnh kim tra xem n c hot ng chnh xc khng. - T nhng b phn nh ny, ta li kim tra nhng unit ln hn c s dng nhng unit nh c kim tra .
Unit Test
- Mt Unit Test l mt phn ca source code, thc thi mt phn code chnh khc v so snh kt qu thc t vi kt qu mong i. - c thc hin bi cc lp trnh vin. - C th lm bng tay (Manual Unit Test) hoc t ng (Automated Unit Test)
10
Cc nh ngha(what):
1. A mock object is simply a debug replacement for a real-world object (Andy Hunt, Dave Thomas Pragmatic unit testing in java with Junit) 2.
A mock object is an object created to stand in for an object that your code will be collaborating with. Your code can call methods on the mock object, which will deliver results as set up by your tests.
(Source: JUnit in Action, Vincent Massol)
11
c im
n gin hn i tng thc nhng vn gi c s tng tc vi cc i tng khc. Khng lp li ni dung i tng thc. Cho php thit lp cc trng thi ring tr gip kim tra.
12
MOCK OBJECTS(tt)
Thit k (how):
3 bc quan trng s dng cc i tng thay th trng kim th: 1. a ra interface m t i tng.
2. Vit ni dung cho i tng thc da trn interface 3. Trch interface t i tng tht v trin khai mock object(MO) da trn interface .
14
MOCK Object
INTERFACE
REAL CLASS
MOCK CLASS
TESTED FUNCTION
15
Cc Dng Mocks
C th dng nhiu k thut test cc module trong chng trnh:
16
Simple Stubs
Gi s gi hm getTime() tr v thi gian hin ti:
public long getTime() { return System.currentTimemillis(); }
17
Mock
Kim th 1 lp truy xut c s d liu:
public class RealDAO { public string getName( long id) { //. } }
To 1 interface:
public interface Environmental { public long getTime(); // Other methods omitted... }
18
MOCK(tt)
p dng class interface cho lp mun kim th: public class SystemEnvironment implements Environmental {
public long getTime() { return System.currentTimeMillis(); } // other methods ...
}
19
MOCK(tt)
To class mock k tha t interface ny:
public void setTime(long aTime) { current_time = aTime; } private long current_time; // ...
}
20
MOCK(tt)
S dng mock kim th cc chc nng lin quan:
public class PrintBill { public static string Print(string name) { string result; if (name == "XDPM") { result = "Xin dung phun em"; } else { result = "Unknown"; } return result; } }
21
MOCK(tt)
[TestFixture] public class PrintTest { [Test] public void XDPMTest() { MockDAO mock = new MockDAO(); string actualValue = PrintBill.Print(mock.GetName(0)); Assert.That(actualValue, Is.EqualTo("Xay dung phan mem")); } }
22
Test a Servlet
L nhng khi lnh ca 1 web server qun l. Yu cu cc URLs xc nh chuyn n container servlet. Cc URL ln lt gi cc code servlet. Sau servlet gi 1 reponse ti cc trnh duyt yu cu.
23
Test a Servlet(tt)
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException { String str_f = req.getParameter("Fahrenheit"); res.setContentType("text/html"); PrintWriter out = res.getWriter(); try { int temp_f = Integer.parseInt(str_f); double temp_c = (temp_f - 32)*5.0 /9.0; out.println("Fahrenheit: " + temp_f + ", Celsius: " + temp_c); g catch (NumberFormatException e) f out.println("Invalid temperature: " + str_f); } }
24
Test a Servler(tt)
Line 1
import junit.framework.* import com.mockobjects.servlet.*; public class TestTempServlet extends TestCase { public void test_bad_parameter() throws Exception { TemperatureServlet s = new TemperatureServlet(); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.setupAddParameter("Fahrenheit", "boo!"); response.setExpectedContentType("text/html"); 15 s.doGet(request,response); response.verify(); assertEquals("Invalid temperature: boo!nn", response.getOutputStreamContents()); } public void test_boil() throws Exception { TemperatureServlet s = new TemperatureServlet(); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.setupAddParameter("Fahrenheit", "212"); response.setExpectedContentType("text/html"); s.doGet(request,response); response.verify(); assertEquals("Fahrenheit: 212, Celsius: 100.0nn", response.getOutputStreamContents());
10
20
25 30
} 35
25
u im 1. m bo tnh chnh xc ca chng trnh 2. Pht hin li ngay t nhng phase u tin trong qu trnh pht trin phn mm 3. Sa i d dng hn 4. Tng kh nng tch hp code
26
Nhc im
1. Gy kh khn trong vic thc hin chuyn i gia i tng tht v i tng thay th(mocks) 2. Khng th lng trc nhng vn c th xy ra khi kt hp cc module vi nhau 3. Phi duy tr m hnh(maintain the mock)
27
Cc cng c h tr
Junit CppUnit Cunit EasyMock HtmlUnit HttpUnit NUnit v.v
29
Demo NUnit
30