Tải bản đầy đủ (.ppt) (18 trang)

Tài liệu Mb Unit Test, Automation Unit Test, Test Driven Design (file ppt) pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (878.34 KB, 18 trang )

Trinh Minh Cuong Microsoft
Vietnam

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
Anh code
quá t !ệ
quá t !ệ
Cô test
Cô test
thi u ế
thi u ế
tr ng ườ
tr ng ườ
h p !!@@#ợ
h p !!@@#ợ
Manual 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
There 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.
Test Driven Design
Documentation 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ó.

….
Integration 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
MbUnit – 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



/>Giới thiệu về MbUnit



/>
/>Hướng dẫn thực hành

Download MbUnit ở đây

/>
Mở solution XLINQ1, xem project TestXLINQ

XLINQ references đến MbUnit.Framework và XLINQ1 (project cần
test)
using 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()
{
}

}
}
Giả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
Cá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
Giao diện chương trình test
Báo cáo kết quả test
DEMO
Tham khảo

Articles:
/> />esting.aspx
/>bUnit.aspx

Books

xUnit Test Patterns – Gerard Meszaros

The Art of Unit Testing - Roy Osherove

Tools

MbUnit and Gallio – www.MbUnit.com


TestDriven.Net – www.TestDriven.net

Rhino Mocks – www.Ayende.com

WatiN –

Xunit - />

×