Translate

Thứ Tư, 4 tháng 12, 2013

UnitTest với Android application

JUnit là gì?
Là framework hỗ trợ thực hiện unit test trong Java.


Unit test là gì?
Unit test là đoạn code viết vởi developer nhằm thực hiện một chức năng cụ thể trong những đoạn code được test. Tỷ lệ code được test bởi unit test thường được gọi là test coverage.
Nên Unit test cái gì?
Nên viết test cho tất cả các phần quan trọng và phức tạp trong ứng dụng. Đừng test những đoạn code kiểu như getter/setter hoặc những đoạn code thuộc về framework và được đảm bảo bởi framework.

Code Junit test để ở đâu?
Các unit test method nên viết ở thư mục/project code riêng biệt so với source code gốc cần test.

JUnit annotations (Junit 4):
Là các chú giải của Junit nhằm chỉ báo một mong muốn đối với một test method nào đó.

JUnit 3 và JUnit 4 Version :
JUnit 3 được hỗ trợ trực tiếp trong Android SDK và Eclipse. Junit 3 yêu cầu các lớp test phải kế thừa từ lớp junit.framework.TestCase và các method test phải có prefix là test. 
TH muốn dùng JUnit 4 thì down thư viện Junit4.x.jar về rồi đưa vào project. 
Junit 3 khác Junit 4 là không hỗ trợ các annotation như @Before, @BeforeClass, .....


AnnotationDescription
@Test
public void method()
The @Test annotation identifies a method as a test method.
@Test (expected = Exception.class)Fails, if the method does not throw the named exception.
@Test(timeout=100)Fails, if the method takes longer than 100 milliseconds.
@Before
public void method()
This method is executed before each test. It is used to can prepare the test environment (e.g. read input data, initialize the class).
@After
public void method()
This method is executed after each test. It is used to cleanup the test environment (e.g. delete temporary data, restore defaults). It can also save memory by cleaning up expensive memory structures.
@BeforeClass
public static void method()
This method is executed once, before the start of all tests. It is used to perform time intensive activities, for example to connect to a database. Methods annotated with this annotation need to be defined asstatic to work with JUnit.
@AfterClass
public static void method()
This method is executed once, after all tests have been finished. It is used to perform clean-up activities, for example to disconnect from a database. Methods annotated with this annotation need to be defined as static to work with JUnit.
@IgnoreIgnores the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included.
StatementDescription
fail(String)Let the method fail. Might be used to check that a certain part of the code is not reached. Or to have a failing test before the test code is implemented. The String parameter is optional.
assertTrue([message], boolean condition)Checks that the boolean condition is true.
assertFalse([message], boolean condition)Checks that the boolean condition is false.
assertEquals([String message], expected, actual)Tests that two values are the same. Note: for arrays the reference is checked not the content of the arrays.
assertEquals([String message], expected, actual, tolerance)Test that float or double values match. The tolerance is the number of decimals which must be the same.
assertNull([message], object)Checks that the object is null.
assertNotNull([message], object)Checks that the object is not null.
assertSame([String], expected, actual)Checks that both variables refer to the same object.
assertNotSame([String], expected, actual)Checks that both variables refer to different objects.
Run UnitTest
Có thể chạy UnitTest bằng Android JUnit Test trong Eclipse hoặc chạy bằng Apache Ant qua commandline (thường dùng để chạy tự động trên 1 server test riêng).

Test xử lý bất đồng bộ (Asynchronous processing)
Test bất đồng bộ là 1 thách thức, cách thông thường là dùng một instance của lớp CountDownLatch trong test code và ra tín hiệu từ luồng bất đồng bộ ra ngoài để thông báo xử lý đã hoàn thành.

Test dựa trên sự kiện hệ thống (system change)
Trong quá trình test chạy có trường hợp cần thay đổi trạng thái hệ thống (ví dụ bật Wifi), những trường hợp như vậy thường không thể thực hiện trực tiếp được bằng test code bởi ứng dụng test chỉ có permission của ứng dụng được test. Cách xử lý là implement và install một ứng dụng khác có đủ permission lên thiết bị và trigger ứng dụng đó từ test code thông qua Intent.

Test bằng các test framework
Có một loạt các framework hỗ trợ viết unit test hay automation test đơn giản hơn. Nên thử:
UnitTest: Robotium, Robolectric, Espresso 
Automation test: Selenium, Selendroid, Webdriver, NativeDriver

Tạo Logging server
Thường các log file của test case nên được lưu trên server chứ ko phải thiết bị và cung cấp khả năng truy cập từ xa đến cho tiện. Vậy nếu có thể hãy tạo 1 server backend và đưa kết quả log test lên đó thông qua HTTP.

Tự động hóa việc test trên test server
Vì test là việc phải chạy đi chạy lại liên tục và thời gian chạy toàn bộ chiếm khá nhiều thời gian nên tốt nhất hãy tạo 1 test server, đưa code ứng dụng và code test lên đó (tốt nhất là auto fetch từ repository server về) và lập lịch tự động run test script. Trên Linux có thể làm việc này với crontab, bash script (sh), và Apache Ant.

Không có nhận xét nào:

Đăng nhận xét