Bạn có thể xác định 0. Với cách tiếp cận này, bạn kết thúc với một phương pháp assertNotRaises , ngoài điều kiện thất bại đảo ngược của nó, hoạt động giống hệt với assertRaises . Show TLDR và bản demo trực tiếpHóa ra thật dễ dàng để thêm một phương thức 4 (tôi mất khoảng 4 lần để viết câu trả lời này như mã đã làm). Đây là bản demo trực tiếp của phương pháp assertNotRaises đang hoạt động. Giống như assertRaises , bạn có thể chuyển một cuộc gọi và tranh luận cho assertNotRaises hoặc bạn có thể sử dụng nó trong một câu lệnh 8. Bản demo trực tiếp bao gồm một trường hợp thử nghiệm chứng minh rằng assertNotRaises hoạt động như dự định.Thông tin chi tiếtViệc triển khai 0 khá phức tạp, nhưng với một chút phân lớp thông minh, bạn có thể ghi đè và đảo ngược tình trạng thất bại của nó.
assertRaises(exception, function, *args, **keywords)3 và trả về nó (xem định nghĩa của nó trong mô -đun assertRaises(exception, function, *args, **keywords)4). Bạn có thể xác định lớp assertRaises(exception, function, *args, **keywords)5 của riêng bạn bằng cách phân lớp assertRaises(exception, function, *args, **keywords)6 và ghi đè lên phương thức assertRaises(exception, function, *args, **keywords)7 của nó:
Thông thường, bạn xác định các lớp trường hợp kiểm tra bằng cách chúng kế thừa từ assertRaises(exception, function, *args, **keywords)8. Nếu bạn thay vào đó kế thừa từ một lớp con assertRaises(exception, function, *args, **keywords)9:
Tất cả các trường hợp thử nghiệm của bạn bây giờ sẽ có sẵn phương pháp Khung kiểm tra đơn vị Unittest được sử dụng để xác nhận rằng mã thực hiện như được thiết kế. Để đạt được điều này, Unittest hỗ trợ một số phương pháp quan trọng theo cách hướng đối tượng: & nbsp;unittest unit testing framework is used to validate that the code performs as designed. To achieve this, unittest supports some important methods in an object-oriented way:
Một cái nhìn sâu sắc hơn cho các điều khoản trên có thể đạt được từ https://www.geeksforgeek.org/unit-testing-python-unittest/ AssertRaises () - Nó cho phép một ngoại lệ được gói gọn, có nghĩa là thử nghiệm có thể ném một ngoại lệ mà không thoát khỏi thực thi, như thông thường cho các trường hợp ngoại lệ chưa được xử lý. Bài kiểm tra vượt qua nếu ngoại lệ được nêu ra, gây ra lỗi nếu một ngoại lệ khác được nêu ra hoặc không thành công nếu không có ngoại lệ nào được nâng lên.– It allows an exception to be encapsulated, meaning that the test can throw an exception without exiting the execution, as is normally the case for unhandled exceptions. The test passes if exception is raised, gives an error if another exception is raised, or fails if no exception is raised. Có hai cách bạn có thể sử dụng AssertRaiese:
Chúng tôi viết một sự bất ổn mà thất bại nếu không có ngoại lệ nào được đưa ra bởi một hàm hoặc khi một ngoại lệ được đưa ra bởi tuyên bố Assert khác với ngoại lệ dự kiến. Do đó, bằng phương pháp này, chúng ta có thể kiểm tra xem một ngoại lệ có được nâng lên bởi một hàm hay không.if no exception is raised by a function or when an exception raised by assert statement is different from expected exception. Thus, by this method we can check if an exception is raised by a function or not. Ví dụ 1 : Python3assertRaises(exception)1 0assertRaises(exception)3 assertRaises(exception)4 assertRaises(exception)5 assertRaises(exception)6 assertRaises(exception)7 assertRaises(exception)8 assertRaises(exception)9 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1 assertRaises(exception)8 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK4 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK5 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK6 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK7 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK8 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK9 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)05050 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)2 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)3 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)4 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)5 Đầu ra: & nbsp; . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK Ở đây, trong đầu ra của người Viking. Trên dòng đầu ra đầu tiên có nghĩa là thử nghiệm đã được vượt qua và hàm đã ném ngoại lệ & nbsp; typeerror trong khi thêm giá trị số nguyên và giá trị chuỗi.TypeError while adding an integer value and a string value. Ví dụ 2: Python3assertRaises(exception)1 0assertRaises(exception)3 assertRaises(exception)4 assertRaises(exception)5 assertRaises(exception)6 assertRaises(exception)7 assertRaises(exception)8 assertRaises(exception)9 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1 assertRaises(exception)8 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1 assertRaises(exception)8 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK8 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK9 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)05050 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)2 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)3 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)4 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)5 Đầu ra: & nbsp; F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1) Ở đây, trong đầu ra của người Viking. Trên dòng đầu ra đầu tiên có nghĩa là thử nghiệm đã được vượt qua và hàm đã ném ngoại lệ & nbsp; typeerror trong khi thêm giá trị số nguyên và giá trị chuỗi. Ví dụ 2: Python3
assertRaises(exception)1 0assertRaises(exception)3 assertRaises(exception)4 assertRaises(exception)5 assertRaises(exception)6 assertRaises(exception)7 assertRaises(exception)8 assertRaises(exception)9 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1 assertRaises(exception)8 assertRaises 3. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK0 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1 assertRaises(exception)8 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK3 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK8 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK9 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)05050 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)2 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)3 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)4 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)5 Đầu ra: . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK Đầu ra: & nbsp;ZeroDivisionError exception. Therefore, in example 3, when 100 is divided by 0 it raises an Exception of type ZeroDivisionError resulting in unittest to pass. Thus “.” in output signifies that the test has been passed. Ở đây, trong đầu ra của người Viking. Trên dòng đầu ra đầu tiên có nghĩa là thử nghiệm đã được vượt qua và hàm đã ném ngoại lệ & nbsp; typeerror trong khi thêm giá trị số nguyên và giá trị chuỗi. Python3assertRaises(exception)1 0assertRaises(exception)3 assertRaises(exception)4 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)4 assertRaises(exception)6 assertRaises(exception)7 assertRaises(exception)8 assertRaises(exception)9 15. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK1 assertRaises(exception)8 18 19 20 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)0 22 23 24 25 26 27. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK8 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK9 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)05050 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)2 33F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)4 F ====================================================================== FAIL: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/5bc65171e57a3294596f5333f4b7ed53.py", line 6, in test_1 1 + 1 AssertionError: Exception not raised ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)5 Đầu ra: E ====================================================================== ERROR: test_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/8520c06939bb0023b4f76f381b2c8cf2.py", line 11, in test_1 file = open("GeeksforGeeks.txt", 'r') FileNotFoundError: [Errno 2] No such file or directory: 'GeeksforGeeks.txt' ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (errors=1) Khi chúng tôi cố gắng mở một tệp không tồn tại, nó sẽ làm tăng một ioerror là lớp phụ của Môi trường. Trong ví dụ trên, Unittest không thành công vì loại ngoại lệ được nâng lên theo chức năng được dự kiến sẽ thuộc loại RunTimEError, thay vào đó, nó đã nêu ra một ngoại lệ của Môi trường. Vì ngoại lệ được nâng cao và ngoại lệ dự kiến là khác nhau, nó tạo ra một lỗi.IOError which is sub class of EnvironmentError. In the above example, the unittest failed because the type of exception to be raised by function was expected to be of type RuntimeError, instead it raised an exception of EnvironmentError. Since the exception raised and exception expected are different, it produces an error. Làm thế nào để bạn khẳng định các ngoại lệ trong bài kiểm tra đơn vị Python?Có hai cách bạn có thể sử dụng AssertRaiese: sử dụng các đối số từ khóa. Chỉ cần vượt qua ngoại lệ, hàm có thể gọi và các tham số của hàm có thể gọi là đối số từ khóa sẽ gợi ra ngoại lệ. Thực hiện một cuộc gọi chức năng sẽ tăng ngoại lệ với một bối cảnh.using keyword arguments. Just pass the exception, the callable function and the parameters of the callable function as keyword arguments that will elicit the exception. Make a function call that should raise the exception with a context.
Assertraise là gì?AssertRaises là một hàm thất bại trừ khi một ngoại lệ được nâng lên bởi một đối tượng của một lớp.Nó chủ yếu được sử dụng trong các kịch bản thử nghiệm để ngăn mã của chúng tôi bị trục trặc.Hãy làm việc với một ví dụ chi tiết để xem hoạt động của Assertraiese.a function that fails unless an exception is raised by an object of a class. It is mostly used in testing scenarios to prevent our code from malfunctioning. Let's work with a detailed example to see the working of assertRaises .
Mục nào trong Python sẽ dừng thử nghiệm đơn vị một cách đột ngột?Một đối tượng ngoại lệ được tạo ra khi tập lệnh Python làm tăng một ngoại lệ.Nếu tập lệnh rõ ràng không xử lý ngoại lệ, chương trình sẽ bị buộc phải chấm dứt đột ngột.exception object is created when a Python script raises an exception. If the script explicitly doesn't handle the exception, the program will be forced to terminate abruptly.
Pyunit có giống như không nhất quán không?Pyunit là một cách dễ dàng để tạo các chương trình thử nghiệm đơn vị và không nhất quán với Python.(Lưu ý rằng docs.python.org sử dụng tên "Unittest", cũng là tên mô -đun.). (Note that docs.python.org uses the name "unittest", which is also the module name.) |