Unit Test: lập trình viên viết chương trình xong rồi tự test lại theo một số trường hợp cơ bản mà anh ta định ra.. Not Unit Test: lập trình viến viết mã, tester chịu trách nhiệm tes
Trang 1Trinh Minh Cuong Microsoft Vietnam
Trang 2 Unit Test: lập trình viên viết chương trình xong rồi
tự test lại theo một số trường hợp cơ bản mà anh ta định ra.
Not Unit Test: lập trình viến viết mã, tester chịu
trách nhiệm test.
Unit Test vs Non Unit Test
Anh code quá tệ !
Cô test
thiếu trường
hợp !!
@@#
Trang 3Manual Unit Test – Test Driven Design
Manual Unit Test:
Tiết kiệm 1 ít thời gian lúc đầu.
Lập trình viên nhớ được case nào thì test.
Càng đến cuối dự án, số lượng test case tăng lên, khả năng bao quát các khả năng của lập trình viên giảm xuống.
Phải văn bản hóa các test case ra Word, Excel Phần mềm phức tạp lên, văn bản test case nhanh chóng bị lạc hậu -> trở nên vô dụng.
Test Driven Design
Viết code đến đâu, thì viết test logic, test case bằng mã lệnh.
Hướng lập trình viên tư duy: viết code để chạy được và test được.
Sử dụng chương trình tự động duyệt qua tất cả test case rồi test.
Xem thêm ở đây http://www.agiledata.org/essays/tdd.html
Trang 4There were many instances of machine-smashing, particularly in
1811-1812 by a group calling themselves the Luddites [Luddites:
A group of English workers who formed in the early 19th Century They opposed and destroyed new textile machines which threatened their traditional jobs.
Không phải ai cũng thích tự động hóa Hình trên, công nhân Anh phá máy dệt vào đầu thế kỷ 19.
Trang 5Test Driven Design
Trang 6Documentation in TDD
Văn bản thiết kế, test case luôn luôn không đồng nhất với mã nguồn Chúng có thể mới hơn, sai khác và lạc hậu so với mã
nguồn.
Mã nguồn phản ánh trung thực nhất tình trạng, tiến độ của phần mềm Tuy nhiên mã nguồn chưa chắc mô tả đầy đủ mục đích, tính năng của phần mềm cần lập trình.
TDD bám sát mã nguồn hiện có để test
TDD đảm bảo:
Code viết ra, thêm mới được test lặp đi lặp lại theo những test case đề ra.
Module, function mới không tạo hiệu ứng phụ với code cũ.
TDD không chắc chắn đảm bảo:
Bao tất cả test case cần có.
….
Trang 7Integration Testing
Tại sao vẫn cần Integration Testing khi đã có
automation unit test (test driven design)
Integration Test:
Ideally written by a tester, however generally written by a developer.
Unhappy scenarios
End-To-End scenarios
Does the code meet customer requirements
Trang 8MbUnit – About the project
Open source project since 2004, see CodeProject.com
Created by Jonathan de Halleux.
Maintained by strong group of 12 people
“Unit Testing on crack” Scott Hanselman
Used on large scale projects
SubSonic
Rhino Mocks
SubText
http://www.mbunit.org
http://weblogs.asp.net/astopford
Trang 9Giới thiệu về MbUnit
http://www.mbunit.org
http://weblogs.asp.net/astopford
http://www.mbunit.org http://weblogs.asp.net/astopford
Trang 10Hướng dẫn thực hành
Download MbUnit ở đây
http://mb-unit.googlecode.com/files/MbUnit-2.4.2.130-Setup.exe
Mở solution XLINQ1, xem project TestXLINQ
XLINQ references đến MbUnit.Framework và XLINQ1 (project cần test)
Trang 11using XLINQ1; //Dự án cần test
using MbUnit.Framework; //Thư viện MbUnit.FrameWork bắt buộc phải có namespace TestXLINQ
{
[ TestFixture ]
public class TestXLINQ
{
[ SetUp ] //Chuẩn bị dữ liệu, khởi tạo đối tượng cần test
public void prepareTest()
{
}
[ Test ] //Hàm test
public void test_FunctionA()
{
}
[ Row (8)] //Bộ giá trị truyền vào hàm test
[ Row (11)]
[ RowTest ]
public void test_FunctionB ( double testPrice)
{
}
[ TearDown ] //Dọn dẹp sau khi test
public void TearDown()
{
}
}
}
Trang 13Giải thích class attribute ở MbUnit
MbUnit Attributes Purpose
[Test] Indentifies of an individual unit test
[RowTest] For a single test, but with multiple different sets of data to execute a
test against Each set of data is then executed separately as a unique test
[TestFixture] Identifies of a group of unit tests, all Tests, and Initializations/Clean
Ups must appear after this declaration
[TestFixtureSetUp] Identifies a method which should be called a single time prior to
executing any test in the Test Class/Test Fixture
[TestFixtureTearDown] Identifies a method in to be called a single time following the
execution of the last test in a TestClass/TestFixture
[SetUp] Identifies a method to be executed each time before a
TestMethod/Test is executed
[TearDown] Identifies a method to be executed each time after a TestMethod/Test
has executed
Trang 14Cách báo lỗi khi test
Assert
Throw exception
if (Convert.ToDouble(cd.Element( "PRICE" ).Value) <= testPrice) {
throw new Exception( "SelectCDPriceHigherThan test
failed!" );
}
Assert.LowerEqualThan(MinNumberOfCountriesInCDCatalog,
IntersectedCountry.Count(),
ErrorMessage); //Nếu có lỗi thì xuất ra thông báo Thông báo sẽ hiện liên ở MbUnit Console
Trang 15Giao diện chương trình test
Trang 16Báo cáo kết quả test
Trang 17DEMO
Trang 18Tham khảo
Articles:
http://www.codeproject.com/KB/architecture/gunit.aspx
http://www.codeproject.com/KB/architecture/
jdhcompositeunittesting.aspx
http://www.codeproject.com/KB/dotnet/
Tutorial_Extending_MbUnit.aspx
Books
Tools