Kiểm thử phần mềm là quá trình trong đó nhà phát triển đảm bảo rằng đầu ra thực tế của phần mềm phù hợp với đầu ra mong muốn bằng cách cung cấp một số đầu vào kiểm tra cho phần mềm. Kiểm thử phần mềm là một bước quan trọng vì nếu được thực hiện đúng cách, nó có thể giúp nhà phát triển tìm ra lỗi trong phần mềm trong khoảng thời gian ngắn hơn. Kiểm thử phần mềm có thể được chia thành hai loại, Kiểm thử thủ công và Kiểm thử tự động. Kiểm thử tự động là việc thực hiện kiểm thử của bạn bằng tập lệnh thay vì con người. Trong bài viết này, chúng ta sẽ thảo luận về một số phương pháp kiểm thử phần mềm tự động với Python. Hãy viết một ứng dụng đơn giản mà chúng tôi sẽ thực hiện tất cả các bài kiểm tra.
Python3
pip install unittest8
pip install unittest9
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK2
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK4
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______2_______6
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______2_______8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______2_______3_______13_______1
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]3
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]6
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5
pip install nose20
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______2_______8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______22_______4
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]1
pip install nose27
pip install nose27
pip install nose29
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1
nose22
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______34_______6
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______2_______8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______22_______4
nose2 -s DIR_ADD DIR_NAME1
pip install nose27
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]1
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1
nose2 -s DIR_ADD DIR_NAME7
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3_______13_______8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______53_______1_______53_______2
nose2 -s DIR_ADD DIR_NAME.TEST_FILE.TEST_CLASS.TEST_NAME3
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______2_______8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______53_______7
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
nose2 -s DIR_ADD DIR_NAME.TEST_FILE.TEST_CLASS.TEST_NAME9
pip install pytest0
pip install pytest1
pip install pytest2
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3
pip install pytest4
pip install pytest0
pip install pytest6
pip install pytest0
pip install pytest8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______72_______0
pip install pytest0
pip install pytest1_______62_______2
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3
py.test5
pip install pytest0
pip install pytest6
pip install pytest0
pip install pytest8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______0_______01
pip install pytest0
pip install pytest1
pip install pytest2
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3_______0_______06
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______22_______4
nose2 -s DIR_ADD DIR_NAME.TEST_FILE.TEST_CLASS.TEST_NAME7
pip install unittest10
pip install unittest11
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest14
pip install unittest15
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest17
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]3
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest21
pip install pytest2
pip install unittest23
pip install pytest2
pip install unittest25
pip install unittest26
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest29
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest31
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest33
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest35
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest37
pip install unittest38
Ghi chú. Để biết thêm thông tin về hàm __repr__[], hãy tham khảo bài viết này. Bây giờ chúng tôi đã sẵn sàng phần mềm của mình, hãy xem cấu trúc thư mục của thư mục dự án của chúng tôi và sau đó, chúng tôi sẽ bắt đầu kiểm tra phần mềm của mình
---Software_Testing |--- __init__.py [to initialize the directory as python package] |--- app.py [our software] |--- tests [folder to keep all test files] |--- __init__.py
Mô-đun 'đơn vị nhất'
Một trong những vấn đề chính với kiểm thử thủ công là nó đòi hỏi thời gian và công sức. Trong thử nghiệm thủ công, chúng tôi kiểm tra ứng dụng qua một số đầu vào, nếu nó không thành công, chúng tôi sẽ ghi lại hoặc chúng tôi gỡ lỗi ứng dụng cho đầu vào kiểm tra cụ thể đó, sau đó chúng tôi lặp lại quy trình. Với unittest, tất cả các đầu vào kiểm tra có thể được cung cấp cùng một lúc và sau đó bạn có thể kiểm tra ứng dụng của mình. Cuối cùng, bạn nhận được một báo cáo chi tiết với tất cả các trường hợp kiểm tra không thành công được chỉ định rõ ràng, nếu có. Mô-đun unittest có cả khung thử nghiệm tích hợp và trình chạy thử nghiệm. Khung thử nghiệm là một bộ quy tắc phải được tuân theo khi viết các trường hợp thử nghiệm, trong khi trình chạy thử nghiệm là một công cụ thực hiện các thử nghiệm này với một loạt các cài đặt và thu thập kết quả. Cài đặt. unittest có sẵn tại PyPI và có thể được cài đặt bằng lệnh sau –
pip install unittest
Sử dụng. Chúng tôi viết các bài kiểm tra trong một mô-đun Python [. py]. Để chạy thử nghiệm, chúng tôi chỉ cần thực thi mô-đun thử nghiệm bằng bất kỳ IDE hoặc thiết bị đầu cuối nào. Bây giờ, hãy viết một số bài kiểm tra cho phần mềm nhỏ của chúng tôi đã thảo luận ở trên bằng cách sử dụng mô-đun unittest
- Tạo một tệp có tên tests. py trong thư mục có tên “kiểm tra”
- trong các bài kiểm tra. py nhập đơn vị nhất
- Tạo một lớp có tên là TestClass kế thừa từ lớp unittest. Trường hợp thử nghiệm. Quy tắc 1. Tất cả các bài kiểm tra được viết dưới dạng các phương thức của một lớp, lớp này phải kế thừa từ lớp unittest. Trường hợp thử nghiệm
- Tạo một phương pháp kiểm tra như hình dưới đây. Quy tắc 2. Tên của mỗi và mọi phương thức thử nghiệm phải bắt đầu bằng "test" nếu không nó sẽ bị bỏ qua bởi người chạy thử nghiệm.
Python3
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1
pip install unittest40
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest44
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest47
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest49_______22_______9
pip install unittest51
pip install unittest52
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest54
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest56
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3_______0_______59
nose2 -s DIR_ADD DIR_NAME1
pip install unittest61
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______0_______63
pip install unittest64
pip install unittest65
- Quy tắc 3. Chúng tôi sử dụng các câu lệnh đặc biệt thay vì các câu lệnh khẳng định tích hợp sẵn trong Python. Đối số đầu tiên của assertEqual[] là đầu ra thực tế, đối số thứ hai là đầu ra mong muốn và đối số thứ ba là thông báo lỗi sẽ được hiển thị trong trường hợp hai giá trị khác nhau [kiểm tra không thành công]
- Để chạy các bài kiểm tra mà chúng ta vừa xác định, chúng ta cần gọi phương thức unittest. main[], thêm các dòng sau vào “tests. mô-đun py.
Python3
pip install unittest10
pip install unittest11
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest14
pip install unittest15
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest73
- Vì những dòng này, ngay khi bạn chạy tập lệnh “test. py”, hàm unittest. main[] sẽ được gọi và tất cả các bài kiểm tra sẽ được thực hiện
Cuối cùng là “bài kiểm tra. mô-đun py” phải giống với mã được cung cấp bên dưới.
Python3
pip install unittest74
pip install unittest75
pip install unittest76
pip install unittest77_______0_______74
pip install unittest79
pip install unittest8
pip install unittest81
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1
pip install unittest40
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3_______13_______8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______0_______47
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest90_______22_______9
pip install unittest65
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______2_______3_______0_______59
nose2 -s DIR_ADD DIR_NAME1
pip install unittest61
pip install unittest98
pip install unittest63
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK00
pip install unittest65
pip install unittest10
pip install unittest11
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest14
pip install unittest15
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest73
Đã viết các trường hợp thử nghiệm của chúng tôi, bây giờ chúng ta hãy kiểm tra ứng dụng của mình xem có lỗi nào không. Để kiểm tra ứng dụng của bạn, bạn chỉ cần thực thi tệp kiểm tra “tests. py” bằng dấu nhắc lệnh hoặc bất kỳ IDE nào bạn chọn. Đầu ra phải giống như thế này
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK
Trong dòng đầu tiên, một. [dấu chấm] đại diện cho một thử nghiệm thành công trong khi 'F' sẽ đại diện cho một trường hợp thử nghiệm thất bại. Cuối cùng, thông báo OK cho chúng tôi biết rằng tất cả các bài kiểm tra đã được thông qua thành công. Hãy thêm một vài bài kiểm tra nữa trong “bài kiểm tra. py” và kiểm tra lại ứng dụng của chúng tôi.
Python3
pip install unittest74
pip install unittest75
pip install unittest76
pip install unittest77_______0_______74
pip install unittest79
pip install unittest8
pip install unittest81
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1
pip install unittest40
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3_______13_______8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______0_______47
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest90_______22_______9
pip install unittest65
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______2_______3_______0_______59
nose2 -s DIR_ADD DIR_NAME1
pip install unittest61
pip install unittest98
pip install unittest63
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK00
pip install unittest65
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK40
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______0_______47
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest90_______2_______47
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK48
pip install unittest65
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______2_______3
pip install unittest59
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK47
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK54
pip install unittest61
pip install unittest98
pip install unittest63
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK58
pip install unittest65
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK62
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3_______13_______8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______0_______47
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest90_______2_______69
pip install unittest65
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______2_______3_______2_______73
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK74_______0_______61
pip install unittest98
pip install unittest63
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK78
pip install unittest65
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK82_______2_______3_______13_______8
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______0_______47
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest90_______2_______47
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK90
pip install unittest65
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______2_______3
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK73
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK47_______2_______54
pip install unittest61
pip install unittest98
pip install unittest63
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]00
pip install unittest65
pip install unittest10
pip install unittest11
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest14
pip install unittest15
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest73
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]
Một vài điều cần lưu ý trong báo cáo thử nghiệm ở trên là -
- Dòng đầu tiên biểu thị rằng thử nghiệm 1 và thử nghiệm 3 được thực hiện thành công trong khi thử nghiệm 2 và thử nghiệm 4 không thành công
- Mỗi trường hợp thử nghiệm thất bại được mô tả trong báo cáo, dòng đầu tiên của mô tả chứa tên của trường hợp thử nghiệm không thành công và dòng cuối cùng chứa thông báo lỗi mà chúng tôi đã xác định cho trường hợp thử nghiệm đó
- Ở cuối báo cáo, bạn có thể thấy số lần kiểm tra không thành công, nếu không có lần kiểm tra nào thất bại, báo cáo sẽ kết thúc bằng OK
Ghi chú. Để có thêm kiến thức, bạn có thể đọc tài liệu đầy đủ của unittest
Mô-đun “mũi2”
Mục đích của nose2 là mở rộng unittest để giúp việc kiểm tra dễ dàng hơn. mũi2 tương thích với các bài kiểm tra được viết bằng khung kiểm tra unittest và có thể được sử dụng để thay thế trình chạy thử nghiệm unittest. Cài đặt. mũi2 có thể được cài đặt từ PyPI bằng lệnh,
pip install nose2
Sử dụng. mũi2 không có bất kỳ khung thử nghiệm nào và chỉ đơn thuần là một trình chạy thử nghiệm tương thích với khung thử nghiệm kém nhất. Do đó, chúng tôi sẽ chạy các bài kiểm tra tương tự mà chúng tôi đã viết ở trên [đối với bài kiểm tra kém nhất] bằng cách sử dụng mũi2. Để chạy thử nghiệm, chúng tôi sử dụng lệnh sau trong thư mục nguồn dự án [“Software_Testing” trong trường hợp của chúng tôi],
nose2
Trong thuật ngữ mũi2, tất cả các mô-đun python [. py] với tên bắt đầu từ “test” [i. e. test_file. py, kiểm tra_1. py] được coi là tệp thử nghiệm. Khi thực thi, nose2 sẽ tìm kiếm tất cả các tệp kiểm tra trong tất cả các thư mục con thuộc một hoặc nhiều danh mục sau,
- đó là các gói python [chứa “__init__. py”]
- có tên bắt đầu bằng “test” sau khi được viết thường, tôi. e. TestFiles, bài kiểm tra
- được đặt tên là “src” hoặc “lib”
đầu tiên, mũi2 tải tất cả các tệp thử nghiệm có trong dự án và sau đó các thử nghiệm được thực hiện. Do đó, với nose2, chúng tôi có quyền tự do phân chia các bài kiểm tra của mình giữa các tệp kiểm tra khác nhau trong các thư mục khác nhau và thực hiện chúng cùng một lúc, điều này rất hữu ích khi xử lý số lượng lớn các bài kiểm tra. Bây giờ chúng ta hãy tìm hiểu về các tùy chọn tùy chỉnh khác nhau được cung cấp bởi nose2 có thể giúp chúng tôi trong quá trình thử nghiệm
- Thay đổi thư mục tìm kiếm – Nếu chúng ta muốn thay đổi thư mục mà nose2 tìm kiếm các tệp thử nghiệm, chúng ta có thể thực hiện việc đó bằng cách sử dụng các đối số dòng lệnh -s hoặc –start-dir như,
nose2 -s DIR_ADD DIR_NAME
- ở đây, DIR_NAME là thư mục mà chúng tôi muốn tìm kiếm các tệp thử nghiệm và, DIR_ADD là địa chỉ của thư mục mẹ của DIR_NAME so với thư mục nguồn dự án [i. e. sử dụng ". /” nếu thư mục kiểm tra nằm trong chính thư mục nguồn của dự án]. Điều này cực kỳ hữu ích khi bạn chỉ muốn kiểm tra một tính năng của ứng dụng tại một thời điểm
- Chạy các trường hợp thử nghiệm cụ thể – Sử dụng nose2, chúng tôi cũng có thể chạy một thử nghiệm cụ thể tại một thời điểm bằng cách sử dụng các đối số dòng lệnh -s và –start-dir như,
nose2 -s DIR_ADD DIR_NAME.TEST_FILE.TEST_CLASS.TEST_NAME
- TEST_NAME. tên của phương pháp kiểm tra
- TEST_CLASS. lớp trong đó phương pháp kiểm tra được định nghĩa
- TEST_FILE. tên của tệp thử nghiệm trong đó trường hợp thử nghiệm được xác định i. e. kiểm tra. py
- DIR_NAME. thư mục chứa tệp kiểm tra
- DIR_ADD. địa chỉ của thư mục mẹ của DIR_NAME so với nguồn dự án
- Chạy thử nghiệm trong một mô-đun duy nhất – mũi2 cũng có thể được sử dụng như unittest bằng cách gọi hàm mũi2. main[] giống như chúng ta đã gọi là unittest. main[] trong các ví dụ trước
Mô-đun “pytest”
pytest là khung thử nghiệm phổ biến nhất cho python. Sử dụng pytest, bạn có thể kiểm tra mọi thứ từ tập lệnh python cơ bản đến cơ sở dữ liệu, API và giao diện người dùng. Mặc dù pytest chủ yếu được sử dụng để kiểm tra API, nhưng trong bài viết này, chúng tôi sẽ chỉ đề cập đến những điều cơ bản về pytest. Cài đặt. Bạn có thể cài đặt pytest từ PyPI bằng lệnh,
pip install pytest
Sử dụng. Người chạy thử nghiệm pytest được gọi bằng cách sử dụng lệnh sau trong nguồn dự án,
py.test
Không giống như nose2, pytest tìm kiếm các tệp thử nghiệm ở tất cả các vị trí bên trong thư mục dự án. Bất kỳ tệp nào có tên bắt đầu bằng “test_” hoặc kết thúc bằng “_test” đều được coi là tệp thử nghiệm theo thuật ngữ pytest. Hãy tạo một tệp “test_file1. py” trong thư mục “tests” làm tệp thử nghiệm của chúng tôi. Tạo phương pháp thử nghiệm. pytest hỗ trợ các phương thức kiểm tra được viết trong khung unittest, nhưng khung pytest cung cấp cú pháp dễ dàng hơn để viết bài kiểm tra. Xem mã bên dưới để hiểu cú pháp phương thức thử nghiệm của khung pytest.
Python3
pip install unittest76
pip install unittest77_______0_______74
pip install unittest79
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]15
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest47
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest90_______22_______9
pip install unittest65
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]23
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]24
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2_______13_______2
nose2 -s DIR_ADD DIR_NAME1
pip install unittest61
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______13_______30
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]31
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]32
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]33
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]34
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]36
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
pip install unittest47
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
pip install unittest90_______2_______47
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK54
pip install unittest65
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]23
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]46
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]2
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK47
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK54
pip install unittest61
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5_______0_______63
.F.F ====================================================================== FAIL: test_area_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 11, in test_area_negative self.assertEqual[sq.area[], -1, f'Area is shown {sq.area[]} rather than -1 for negative side length'] AssertionError: 9 != -1 : Area is shown 9 rather than -1 for negative side length ====================================================================== FAIL: test_perimeter_negative [__main__.TestSum] ---------------------------------------------------------------------- Traceback [most recent call last]: File "tests_unittest.py", line 19, in test_perimeter_negative self.assertEqual[sq.perimeter[], -1, f'Perimeter is {sq.perimeter[]} rather than -1 for negative side length'] AssertionError: -24 != -1 : Perimeter is -24 rather than -1 for negative side length ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED [failures=2]54
Ghi chú. tương tự như unittest, pytest yêu cầu tất cả các tên test bắt đầu bằng “test”. Không giống như unittest, pytest sử dụng các câu lệnh xác nhận python mặc định giúp sử dụng dễ dàng hơn. Lưu ý rằng, bây giờ thư mục “kiểm tra” chứa hai tệp có tên là “kiểm tra. py” [viết bằng unittest framework] và “test_file1. py” [được viết bằng khung pytest]. Bây giờ hãy chạy chương trình thử nghiệm pytest
py.test
Bạn sẽ nhận được một báo cáo tương tự như thu được bằng cách sử dụng unittest
pip install unittest1
Phần trăm ở phía bên phải của báo cáo cho biết phần trăm bài kiểm tra đã được hoàn thành tại thời điểm đó, tôi. e. 2 trong số 6 trường hợp thử nghiệm đã được hoàn thành khi kết thúc “test_file1. py”. Dưới đây là một vài tùy chỉnh cơ bản đi kèm với pytest