Hướng dẫn python-unittest github - python-github đơn nhất
2.2. Mục đích 4.3. Ví dụ bắt đầu4.4. Giao diện Command-Line 4.5. Test Discovery import unittest class FooTest(unittest.TestCase): """Sample test case""" # preparing to test def setUp(self): """ Setting up for the test """ print "FooTest:setUp_:begin" ## do something... print "FooTest:setUp_:end" # ending the test def tearDown(self): """Cleaning up after the test""" print "FooTest:tearDown_:begin" ## do something... print "FooTest:tearDown_:end" # test routine A def testA(self): """Test routine A""" print "FooTest:testA" # test routine B def testB(self): """Test routine B""" print "FooTest:testB"
4.4. Giao diện Command-LineCó thể sử dụng giao diện command-line để chạy test từ modules, lớp hoặc từng phương thức test bất kỳ: python -m unittest test_module1 test_module2 python -m unittest test_module.TestClass python -m unittest test_module.TestClass.test_method Các options: -b, -c, -f có thể thấy được khi chạy: 4.5. Test DiscoveryUnittest hỗ trợ test discorvery đơn giản - cho phép chạy nhiều test cùng một lúc. 4.6. Tổ chức test code
4.7. Sử dụng lại test code4.8. Bỏ qua test và dự kiến lỗi
*Các lớp và hàm- _ Here
assertIsNone(x)x is NoneassertIsNotNone(x)x is not None assertIn(a, b)a in b assertNotIn(a, b)
5.1.3. Sự khác nhau giữa Mocks và Stubs4 kiểu Test Double:
With fakes and stubs, you can test if the test subject called the right method with the right input. You can test how the subject handles the result and how it reacts to an error or exception. These tests are known as state verification. But what if you want to know if the test subject called the same method twice? What if you want to know if it called several methods in the proper order? Such tests are known as behavior verification, and to do them, you need mocks. 5.1.4. Classical and Mockist Testing
5.2. Mock trong PythonNote: import unittest class SimpleWidgetTestCase(unittest.TestCase): def setUp(self): self.widget = Widget('The widget') def test_default_widget_size(self): self.assertEqual(self.widget.size(), (50,50), 'incorrect default size') def test_widget_resize(self): self.widget.resize(100,150) self.assertEqual(self.widget.size(), (100,150), 'wrong size after resize')5 có trong bộ thư viện chuẩn từ Python3.3. Còn từ 3.3 đổ về 2.7, có trong thư viện `unittest.mock
5.3. Mock class5.4. Cấu trúc cơ bản của Mock classdef suite(): suite = unittest.TestSuite() suite.addTest(WidgetTestCase('test_default_size')) suite.addTest(WidgetTestCase('test_resize')) return suite0 class(green) có 2 lớp cha def suite(): suite = unittest.TestSuite() suite.addTest(WidgetTestCase('test_default_size')) suite.addTest(WidgetTestCase('test_resize')) return suite1 và def suite(): suite = unittest.TestSuite() suite.addTest(WidgetTestCase('test_default_size')) suite.addTest(WidgetTestCase('test_resize')) return suite2. def suite(): suite = unittest.TestSuite() suite.addTest(WidgetTestCase('test_default_size')) suite.addTest(WidgetTestCase('test_resize')) return suite1 xác định ra routine cần thiết bằng các mock object. Nó sẽ ghi đè 1 số magic methods, cho chúng behavior mặc định? Và nó cũng cung cấp assert routines nhằm theo dõi, lần theo behavior của mock. Đối với def suite(): suite = unittest.TestSuite() suite.addTest(WidgetTestCase('test_default_size')) suite.addTest(WidgetTestCase('test_resize')) return suite2, nó cập nhật các magic methods giúp cho mock object có thể gọi được. 6. Tài liệu tham khảo
|