Hướng dẫn python testing with pytest github - thử nghiệm python với pytest github
Show
Sử dụng các tùy chọn
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2 $ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
2. Viết chức năng kiểm traKiểm tra một gói
Các hàm và đồ đạc móc được sử dụng bởi các thử nghiệm trong nhiều thư mục con phải được chứa trong |
Sử dụng các câu lệnh def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 212 | Tuyên bố Python def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 212 bình thường là công cụ chính của bạn để truyền đạt lỗi kiểm tra |
---|---|
Sau đây là danh sách một vài trong số các biểu mẫu def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 212 và các hàm trợ giúp def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 212: | pytest |
nhất quán | $ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================07 |
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================08 | $ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================09 |
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
10$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
11
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
12
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
- Bạn có thể sử dụng
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
13 với bất kỳ biểu thức nào$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
18- Nếu biểu thức sẽ đánh giá thành
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
14 nếu được chuyển đổi thành$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
15, thử nghiệm sẽ thất bại
pytest bao gồm một tính năng gọi là def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 212 viết lại để chặn các cuộc gọi def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 212 và thay thế chúng bằng một cái gì đó có thể cho bạn biết thêm về lý do tại sao các xác nhận của bạn không thành công
- Nhìn thấy:
- Bản demo về báo cáo thất bại của Python với pytest
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
20
- Bản demo về báo cáo thất bại của Python với pytest
def test_add_raises(): """add() should raise an exception with wrong type param.""" with pytest.raises(TypeError): tasks.add(task="not a Task object")
- Mong đợi ngoại lệ
- Kiểm tra loại ngoại lệ
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
21
- Kiểm tra loại ngoại lệ
def test_start_tasks_db_raises(): """Make sure unsupported db raises an exception.""" with pytest.raises(ValueError) as excinfo: tasks.start_tasks_db("some/great/path", "mysql") exception_msg = excinfo.value.args[0] assert exception_msg == "db_type must be a 'tiny' or 'mongo'"
- Sử dụng loại sai trong chức năng kiểm tra để cố ý gây ra các ngoại lệ
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
19 và sử dụng$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
20
Kiểm tra các tham số đến ngoại lệ
- Bạn có thể kiểm tra để đảm bảo thông báo ngoại lệ là chính xác bằng cách thêm
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
21 - Xem:
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
22$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
23 to some of the tests
@pytest.mark.smoke def test_list_raises(): """One marker: 'smoke'""" ... @pytest.mark.get @pytest.mark.smoke def test_get_raises(): """Two markers: 'get' and 'smoke'""" ...
- Đánh dấu các chức năng kiểm tra
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
24
$ pytest -v -m 'smoke or get' test_api_exceptions.py ============================= test session starts ============================== ... collected 7 items / 5 deselected / 2 selected test_api_exceptions.py::test_list_raises PASSED [ 50%] test_api_exceptions.py::test_get_raises PASSED [100%]
Một bài kiểm tra có thể có nhiều hơn một điểm đánh dấu và một điểm đánh dấu có thể có trong nhiều bài kiểm tra
- Ví dụ: để thêm bộ thử nghiệm khói vào dự án nhiệm vụ, chúng tôi có thể thêm
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
23 vào một số thử nghiệm $ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
25 và$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
26 Các điểm đánh dấu cho phép bạn bỏ qua các bài kiểm tra mà bạn không muốn chạy$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
25- Xem:
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
31
- Xem:
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 20
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 21
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
26- Giả sử chúng tôi quyết định thử nghiệm phải hợp lệ và chúng tôi dự định sẽ thực hiện công việc đó trong phiên bản 0.2.0 của gói
- Biểu thức chúng ta chuyển vào
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
33 có thể là bất kỳ biểu thức python hợp lệ nào - Xem:
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
34
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 22
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 23
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
35- Không bắt buộc trong
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
25, nhưng nó được yêu cầu trong$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
26 - Hiển thị lý do bỏ qua trong đầu ra thử nghiệm với
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
38
- Không bắt buộc trong
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 24
Đánh dấu các bài kiểm tra như mong đợi sẽ thất bại
- Với điểm đánh dấu
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
27, chúng tôi đang nói với Pytest để chạy chức năng kiểm tra, nhưng chúng tôi hy vọng nó sẽ thất bại$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
27 marker, we are telling pytest to run a test function, but that we expect it to fail - Xem:
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
40
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 25
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 26
- Bạn có thể định cấu hình pytest để báo cáo các bài kiểm tra vượt qua nhưng được đánh dấu bằng
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
27 để được báo cáo là$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
42- Xong trong tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
85:
- Xong trong tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 27
Chạy một tập hợp các bài kiểm tra
Một thư mục duy nhất
- Để chạy tất cả các thử nghiệm từ một thư mục, hãy sử dụng thư mục làm tham số để pytest
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
44
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 28
- Sử dụng
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
54 cung cấp cho bạn cú pháp về cách chạy một thư mục, lớp và kiểm tra cụ thể
Một tệp/mô -đun kiểm tra duy nhất
- Để chạy một tệp đầy đủ các thử nghiệm, hãy liệt kê tệp với đường dẫn tương đối dưới dạng tham số
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
46
Một chức năng kiểm tra duy nhất
- Để chạy một chức năng kiểm tra duy nhất, thêm
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
47 và tên chức năng kiểm tra$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
48
Một lớp kiểm tra duy nhất
- Các lớp kiểm tra là một cách để các bài kiểm tra nhóm có ý nghĩa khi được nhóm lại với nhau
- Xem:
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
22
- Xem:
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 29
- Để chỉ chạy lớp này, thêm
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
47, sau đó tên lớp vào tham số tệp$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
51
- Lưu ý: Chạy pylint ở trên sẽ dẫn đến tin nhắn
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
52- Khắc phục dưới dạng Folllows:
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================0
Một phương pháp kiểm tra duy nhất của một lớp kiểm tra
- Nếu bạn muốn chỉ chạy một phương thức, hãy thêm một phương thức khác và tên phương thức
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
54
Một tập hợp các bài kiểm tra dựa trên tên thử nghiệm
- Tùy chọn
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
33 cho phép bạn vượt qua biểu thức để chạy các bài kiểm tra có tên nhất định được chỉ định bởi biểu thức như một chuỗi con của tên thử nghiệm- Bạn có thể sử dụng
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
56,$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
57 và$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
58 trong biểu thức của bạn
- Bạn có thể sử dụng
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================1
Kiểm tra tham số
- Kiểm tra tham số là một cách để gửi nhiều bộ dữ liệu qua cùng một thử nghiệm và có báo cáo pytest nếu bất kỳ bộ nào không thành công
- Chúng ta có thể sử dụng
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
59 để truyền nhiều dữ liệu thông qua cùng một bài kiểm tra- Đối số đầu tiên là một chuỗi có danh sách tên được phân tách bằng dấu phẩy
- Đối số thứ hai là danh sách các giá trị
- pytest sẽ chạy thử nghiệm này một lần cho mỗi giá trị và báo cáo từng là một thử nghiệm riêng biệt
- Xem:
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
60
Một tham số
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================2
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================3
Nhiều tham số
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================4
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================5
- Bạn có thể sử dụng toàn bộ định danh kiểm tra đó (nút) để chạy lại bài kiểm tranode) to re-run a test
- Sử dụng báo giá nếu có khoảng trống trong định danh
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================6
Đối số được gán cho một biến
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================7
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================8
def test_fail():
a = 1
print("--- a is " + str(a))
assert a == 2
13 Tham số tùy chọn để tạo số nhận dạng của riêng chúng tôi
- Sử dụng tham số tùy chọn
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
13 chodef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
14 để tạo số nhận dạng của riêng chúng tôi cho mỗi tập dữ liệu- một danh sách các chuỗi có cùng độ dài với số lượng bộ dữ liệu
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================9
tasks_proj/
├── CHANGELOG.rst
├── LICENSE
├── MANIFEST.in
├── README.rst
├── setup.py
├── src
│ └── tasks
│ ├── __init__.py
│ ├── api.py
│ ├── cli.py
│ ├── config.py
│ ├── tasksdb_pymongo.py
│ └── tasksdb_tinydb.py
└── tests
├── conftest.py
├── pytest.ini
├── func
│ ├── __init__.py
│ ├── test_add.py
│ └── ...
└── unit
├── __init__.py
├── test_task.py
└── ...
0def test_fail():
a = 1
print("--- a is " + str(a))
assert a == 2
14 trên các lớp học
- Các bộ dữ liệu tương tự sẽ được gửi đến tất cả các phương thức kiểm tra trong lớp
tasks_proj/
├── CHANGELOG.rst
├── LICENSE
├── MANIFEST.in
├── README.rst
├── setup.py
├── src
│ └── tasks
│ ├── __init__.py
│ ├── api.py
│ ├── cli.py
│ ├── config.py
│ ├── tasksdb_pymongo.py
│ └── tasksdb_tinydb.py
└── tests
├── conftest.py
├── pytest.ini
├── func
│ ├── __init__.py
│ ├── test_add.py
│ └── ...
└── unit
├── __init__.py
├── test_task.py
└── ...
1tasks_proj/
├── CHANGELOG.rst
├── LICENSE
├── MANIFEST.in
├── README.rst
├── setup.py
├── src
│ └── tasks
│ ├── __init__.py
│ ├── api.py
│ ├── cli.py
│ ├── config.py
│ ├── tasksdb_pymongo.py
│ └── tasksdb_tinydb.py
└── tests
├── conftest.py
├── pytest.ini
├── func
│ ├── __init__.py
│ ├── test_add.py
│ └── ...
└── unit
├── __init__.py
├── test_task.py
└── ...
2Xác định các tham số với def test_fail():
a = 1
print("--- a is " + str(a))
assert a == 2
15
- Xác định các tham số bằng cách bao gồm một
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
15 ngay bên cạnh giá trị tham số- Cú pháp:
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
67 - hữu ích khi
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
15 không thể được lấy từ giá trị tham số
- Cú pháp:
tasks_proj/
├── CHANGELOG.rst
├── LICENSE
├── MANIFEST.in
├── README.rst
├── setup.py
├── src
│ └── tasks
│ ├── __init__.py
│ ├── api.py
│ ├── cli.py
│ ├── config.py
│ ├── tasksdb_pymongo.py
│ └── tasksdb_tinydb.py
└── tests
├── conftest.py
├── pytest.ini
├── func
│ ├── __init__.py
│ ├── test_add.py
│ └── ...
└── unit
├── __init__.py
├── test_task.py
└── ...
3tasks_proj/
├── CHANGELOG.rst
├── LICENSE
├── MANIFEST.in
├── README.rst
├── setup.py
├── src
│ └── tasks
│ ├── __init__.py
│ ├── api.py
│ ├── cli.py
│ ├── config.py
│ ├── tasksdb_pymongo.py
│ └── tasksdb_tinydb.py
└── tests
├── conftest.py
├── pytest.ini
├── func
│ ├── __init__.py
│ ├── test_add.py
│ └── ...
└── unit
├── __init__.py
├── test_task.py
└── ...
43. Đồ đạc pytest
- Đồ đạc là các hàm được chạy bởi pytest trước (và đôi khi sau) các chức năng kiểm tra thực tế
- Cơ chế pytest cung cấp để cho phép tách mã "sẵn sàng cho" và "dọn dẹp sau" từ các chức năng kiểm tra của bạn
- Bạn có thể sử dụng đồ đạc để
- Nhận bộ dữ liệu cho các bài kiểm tra hoạt động
- đưa một hệ thống vào trạng thái đã biết trước khi chạy thử nghiệm
- Chuẩn bị sẵn sàng dữ liệu cho nhiều bài kiểm tra
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
69 Người trang trí được sử dụng để nói với Pytest rằng một chức năng là một vật cố- Khi bạn đưa tên đồ cố định trong danh sách tham số của chức năng kiểm tra, Pytest biết chạy nó trước khi chạy thử nghiệm
tasks_proj/
├── CHANGELOG.rst
├── LICENSE
├── MANIFEST.in
├── README.rst
├── setup.py
├── src
│ └── tasks
│ ├── __init__.py
│ ├── api.py
│ ├── cli.py
│ ├── config.py
│ ├── tasksdb_pymongo.py
│ └── tasksdb_tinydb.py
└── tests
├── conftest.py
├── pytest.ini
├── func
│ ├── __init__.py
│ ├── test_add.py
│ └── ...
└── unit
├── __init__.py
├── test_task.py
└── ...
5$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
70 có tên của vật cố,$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
71, dưới dạng tham số- pytest sẽ tìm kiếm một vật cố với tên này trong mô -đun của bài kiểm tra
- hoặc trong các tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
87 nếu nó không tìm thấy nó trong tệp này
Chia sẻ đồ đạc thông qua conftest.py
- Để chia sẻ đồ đạc giữa nhiều tệp thử nghiệm
- Sử dụng tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
87 ở đâu đó nằm ở trung tâm cho tất cả các bài kiểm tradef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
87 file somewhere centrally located for all of the tests - Đối với dự án nhiệm vụ:
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
74
- Sử dụng tệp
- Bạn có thể đặt đồ đạc trong các tệp kiểm tra riêng lẻ
- chỉ được sử dụng bởi các bài kiểm tra trong tệp đó
- Bạn có thể có các tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
87 khác trong thư mục con của thư mục thử nghiệm hàng đầu- sẽ có sẵn để kiểm tra trong thư mục và thư mục con đó
- Mặc dù
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
87 là mô -đun Python, nhưng nó không được nhập bằng các tệp thử nghiệm- được đọc bởi pytest và được coi là một plugin cục bộ
Sử dụng đồ đạc để thiết lập và phá vỡ
- pytest bao gồm một vật cố định gọi là
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
18 mà chúng ta có thể sử dụng để thử nghiệm và không phải lo lắng về việc dọn dẹpdef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
18 that we can use for testing and don't have to worry about cleaning up
tasks_proj/
├── CHANGELOG.rst
├── LICENSE
├── MANIFEST.in
├── README.rst
├── setup.py
├── src
│ └── tasks
│ ├── __init__.py
│ ├── api.py
│ ├── cli.py
│ ├── config.py
│ ├── tasksdb_pymongo.py
│ └── tasksdb_tinydb.py
└── tests
├── conftest.py
├── pytest.ini
├── func
│ ├── __init__.py
│ ├── test_add.py
│ └── ...
└── unit
├── __init__.py
├── test_task.py
└── ...
6- Nếu có
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
78 trong hàm$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
78 in the function- Thực hiện cố định dừng lại ở đó
- vượt qua kiểm soát các bài kiểm tra
- nhận trên dòng tiếp theo sau khi các bài kiểm tra được thực hiện
- Mã sau khi
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
78 được đảm bảo chạy bất kể điều gì xảy ra trong các bài kiểm tra - Chúng tôi sẽ không trả lại bất kỳ dữ liệu nào với
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
78 trong trận đấu này, nhưng bạn có thể - Xem:
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
88
Truy tìm thực hiện vật cố với def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 216
- Sử dụng
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
16 để xem các đồ đạc nào được chạy
tasks_proj/
├── CHANGELOG.rst
├── LICENSE
├── MANIFEST.in
├── README.rst
├── setup.py
├── src
│ └── tasks
│ ├── __init__.py
│ ├── api.py
│ ├── cli.py
│ ├── config.py
│ ├── tasksdb_pymongo.py
│ └── tasksdb_tinydb.py
└── tests
├── conftest.py
├── pytest.ini
├── func
│ ├── __init__.py
│ ├── test_add.py
│ └── ...
└── unit
├── __init__.py
├── test_task.py
└── ...
7tasks_proj/
├── CHANGELOG.rst
├── LICENSE
├── MANIFEST.in
├── README.rst
├── setup.py
├── src
│ └── tasks
│ ├── __init__.py
│ ├── api.py
│ ├── cli.py
│ ├── config.py
│ ├── tasksdb_pymongo.py
│ └── tasksdb_tinydb.py
└── tests
├── conftest.py
├── pytest.ini
├── func
│ ├── __init__.py
│ ├── test_add.py
│ └── ...
└── unit
├── __init__.py
├── test_task.py
└── ...
8$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
84 và$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
85 phía trước tên trận đấu cho biết phạm vi$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
84 cho phạm vi chức năng$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
85 cho phạm vi phiên
Sử dụng đồ đạc cho dữ liệu thử nghiệm
- Đồ đạc là một nơi tuyệt vời để lưu trữ dữ liệu để sử dụng để thử nghiệm
- Bạn có thể trả lại bất cứ điều gì
- Khi một ngoại lệ xảy ra trong một trận đấu:
tasks_proj/
├── CHANGELOG.rst
├── LICENSE
├── MANIFEST.in
├── README.rst
├── setup.py
├── src
│ └── tasks
│ ├── __init__.py
│ ├── api.py
│ ├── cli.py
│ ├── config.py
│ ├── tasksdb_pymongo.py
│ └── tasksdb_tinydb.py
└── tests
├── conftest.py
├── pytest.ini
├── func
│ ├── __init__.py
│ ├── test_add.py
│ └── ...
└── unit
├── __init__.py
├── test_task.py
└── ...
9Sử dụng nhiều đồ đạc
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
88:
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py0
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
89:
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py1
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py2
Chỉ định phạm vi cố định
- Đồ đạc bao gồm một tham số tùy chọn được gọi là
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
90, điều khiển tần suất thiết lập được thiết lập và phá hủy$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
90, which controls how often a fixture gets set up and torn down$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
91- Chạy một lần trên mỗi chức năng kiểm tra
- Phạm vi mặc định được sử dụng khi không có tham số
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
90 được chỉ định
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
93- Chạy một lần cho mỗi lớp kiểm tra, bất kể có bao nhiêu phương pháp kiểm tra trong lớp
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
94- Chạy một lần cho mỗi mô -đun, bất kể có bao nhiêu chức năng kiểm tra hoặc phương pháp hoặc các đồ đạc khác trong mô -đun sử dụng nó
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
95- Chạy một lần mỗi phiên
- Tất cả các phương pháp kiểm tra và chức năng
- Phạm vi được đặt ở định nghĩa của một vật cố, và không phải ở nơi được gọi là
- Các chức năng kiểm tra sử dụng vật cố không kiểm soát tần suất thiết lập và phá hủy
- Đồ đạc chỉ có thể phụ thuộc vào các đồ đạc khác trong cùng phạm vi hoặc rộng hơn
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
88:
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py3
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
89:
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py1
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py5
Chỉ định đồ đạc với usedfixtures
- Bạn cũng có thể đánh dấu một bài kiểm tra hoặc một lớp có
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
98- Lấy một chuỗi bao gồm một danh sách các đồ đạc được phân tách bằng dấu phẩy để sử dụng
- Một thử nghiệm sử dụng vật cố do
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
99 không thể sử dụng giá trị trả về của trận đấu - Xem:
00tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
Sử dụng def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 217 cho đồ đạc luôn được sử dụng
- Bạn có thể sử dụng
02 để có được một trận đấu để chạy mọi lúctasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Xem:
03tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
Đổi tên đồ đạc
- pytest cho phép bạn đổi tên đồ đạc bằng tham số
04 thànhtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
69
04 parameter totasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
69
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py6
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py7
- Sử dụng tùy chọn pytest
06 để tìm ra nơitasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
07 được xác địnhtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Danh sách tất cả các đồ đạc có sẵn cho bài kiểm tra, bao gồm cả những thiết bị đã được đổi tên
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py8
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py9
Đồ đạc tham số
08tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...0
09 là một vật cố tích hợp đại diện cho trạng thái gọi của vật cốtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Có một trường
10 được điền vào một phần tử từ danh sách được gán chotasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
11 trongtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
12tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Có một trường
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...1
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
13: Danh sách ID chuỗi mỗi tương ứng với các thông số sao cho chúng là một phần của ID kiểm tra
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...2
- Chúng tôi cũng có thể đặt tham số
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
13 thành một hàm cung cấp các định danh
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...3
- Vì tham số là danh sách các đối tượng
15,tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
16 sẽ được gọi với một đối tượngtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
15, cho phép chúng tôi sử dụng các phương thức truy cậptasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
18 để truy cập một đối tượngtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
15 để tạo định danh cho một đối tượngtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
15 tại một thời điểmtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
4. Đồ đạc tích hợp
Sử dụng def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 218 và def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 219
- Các thiết bị tích hợp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
18 vàdef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
19 được sử dụng để tạo thư mục hệ thống tệp tạm thời trước khi thử nghiệm của bạn và xóa thư mục khi bài kiểm tra của bạn kết thúc def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
18- để tạo tệp hoặc thư mục được sử dụng bởi một bài kiểm tra
- Phạm vi chức năng
- Giá trị được trả về từ
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
18 là một đối tượng của loại
27tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
19- Để thiết lập một thư mục cho nhiều bài kiểm tra
- phạm vi phiên
29: Tạo một thư mụctasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
30: Trả về thư mục cơ sở được sử dụng cho phiên nàytasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Thư mục cơ sở bị bỏ lại một mình sau một phiên, nhưng chỉ một số thư mục cơ sở tạm thời gần đây nhất được để lại trên hệ thống
31 để chỉ định thư mục cơ sở của riêng bạntasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Xem:
32tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Thư mục tạm thời cho các phạm vi khác
- Tạo một vật cố khác của phạm vi chúng tôi muốn và sử dụng nó
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
19 - ví dụ: đặt một vật cố phạm vi phạm vi
34 trong chính mô -đun hoặc trong tệptasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
87 - hiểu:
36tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
37tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Tạo một vật cố khác của phạm vi chúng tôi muốn và sử dụng nó
Sử dụng def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 220
- Với bộ đồ được xây dựng
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
20, bạn có thể kiểm soát cách chạy nhất chạy qua các đối số và tùy chọn dòng lệnh, các tệp cấu hình, plugin và thư mục mà bạn đã khởi chạy pytest - Phím tắt đến
40tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Đôi khi được gọi trong tài liệu pytest là "đối tượng cấu hình pytest"
- Thêm tùy chọn dòng lệnh tùy chỉnh và đọc giá trị tùy chọn từ trong thử nghiệm
- Đọc giá trị của các tùy chọn dòng lệnh trực tiếp từ
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
20 - Thêm tùy chọn và có pytest phân tích cú pháp nó bằng cách sử dụng hàm hook
- Nên được thực hiện thông qua các plugin hoặc trong tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
87 ở đầu cấu trúc thư mục dự án của bạn - Xem:
43tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Nên được thực hiện thông qua các plugin hoặc trong tệp
- Đọc giá trị của các tùy chọn dòng lệnh trực tiếp từ
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...4
- Bạn có thể sau đó
- Tùy chọn truy cập từ một bài kiểm tra
- Truy cập
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
20 từ một trận đấu - Tạo vật cố cho tên tùy chọn
- Truy cập các tùy chọn tích hợp cũng như thông tin về cách bắt đầu pytest (thư mục, các đối số, v.v.)
- Xem:
45tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...5
Sử dụng def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 221
- Đôi khi chuyển thông tin từ phiên kiểm tra này sang phiên tiếp theo có thể khá hữu ích
- với bộ đồ xây dựng
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
21
- với bộ đồ xây dựng
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
21 được sử dụng cho chức năngdef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
51 vàdef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
53
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...6
- Giao diện cho trận đấu
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
21:
52tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
53tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Theo quy ước, tên khóa bắt đầu với tên của ứng dụng hoặc plugin của bạn, theo sau là
54 và tiếp tục phân tách các phần của tên khóa vớitasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
54Stasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Giá trị bạn lưu trữ có thể là bất cứ thứ gì có thể chuyển đổi thành JSON
- Ví dụ: Một trận đấu ghi lại các bài kiểm tra mất bao lâu, lưu thời gian và trong lần chạy tiếp theo, báo cáo một lỗi trong các bài kiểm tra mất nhiều thời gian hơn, nói, gấp đôi so với lần trước
- Xem:
56tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Xem thêm:
57tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Xem:
Sử dụng def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 222
- Làn trình
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
22 Buildin cung cấp hai bit chức năng:- Truy xuất
60 vàtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
61 từ một số mãtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Tạm thời vô hiệu hóa việc chụp đầu ra
- Truy xuất
60 vàtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
61 bị bắt được lấy từtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
64tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Giá trị trả lại là bất cứ thứ gì đã được nắm bắt kể từ khi bắt đầu hàm, hoặc từ lần cuối cùng nó được gọi
- Xem
65tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Sử dụng
66 để tạm thời cho đầu ra vượt qua cơ chế bắt giữtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
Sử dụng def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 223
- "Bản vá khỉ" là sửa đổi động của lớp hoặc mô -đun trong thời gian chạy
- Một cách thuận tiện để tiếp quản một phần môi trường thời gian chạy của mã được kiểm tra và thay thế phụ thuộc đầu vào hoặc phụ thuộc đầu ra với các đối tượng hoặc chức năng thuận tiện hơn để kiểm tra
- Lịch trình
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
23 cung cấp các chức năng sau:
69: Đặt thuộc tínhtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
70: Xóa thuộc tínhtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
71: Đặt mục từ điểntasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
72: Xóa mục từ điểntasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
73: Đặt một biến môi trườngtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
74: Xóa biến môi trườngtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
75: Đường dẫn dành cho Sys.Path, đó là danh sách các vị trí nhập khẩu của Pythontasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
76: Thay đổi thư mục làm việc hiện tạitasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Xem: https://docs.pytest.org/en/latest/reference.html#monkeypatch
- Xem xét (xem:
77):tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...7
78 là một hàm không có tham số và không trả về bất cứ điều gìtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- có tác dụng phụ mà chúng ta có thể kiểm tra - nó ghi một tệp vào thư mục nhà của người dùng hiện tại
- Bản vá
79 để mọi thứ trong mô -đuntasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
80 gọitasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
81 có biểu thức Lambda của chúng tôi thay thếtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Xem:
82 trongtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
83tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...8
- Sử dụng
84 để thay đổi các mục từ điển chỉ trong suốt thời gian thử nghiệmtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Xem:
85 trongtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
83tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Xem:
75tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Đặt đường dẫn mới của bạn ở đầu dòng để nhập thư mục nhập mô -đun
- Một cách sử dụng sẽ là thay thế một mô-đun hoặc gói toàn hệ thống bằng phiên bản sơ khai và mã được kiểm tra sẽ tìm thấy phiên bản sơ khai trước tiên
76tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- thay đổi thư mục làm việc hiện tại trong quá trình thử nghiệm
- Hữu ích cho việc kiểm tra các tập lệnh dòng lệnh và các tiện ích khác phụ thuộc vào thư mục làm việc hiện tại là gì bằng cách thiết lập một thư mục tạm thời với bất kỳ nội dung nào có ý nghĩa đối với tập lệnh của bạn
- Bạn cũng có thể sử dụng các chức năng của
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
23 kết hợp với
90 để thay thế các thuộc tính bằng các đối tượng giảtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
Sử dụng def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 224
- Mô -đun
92 là một phần của thư viện Python tiêu chuẩn và cho phép bạn đặt ít ví dụ về mã bên trong tài liệu cho một chức năng và kiểm tra chúng để đảm bảo chúng hoạt độngtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Bạn có thể có tìm kiếm và chạy các bài kiểm tra DocTest trong mã Python của bạn bằng cách sử dụng cờ
93tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Với trận đấu
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
24, bạn có thể xây dựng các đồ đạcdef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
17 để thêm các ký hiệu cho sử dụng pytest không gian tên trong khi chạy các bài kiểm tra tài liệu- thường được sử dụng để thêm nhập mô -đun vào không gian tên
- Xem:
96tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Vì tên
97 dài, chúng tôi quyết định sử dụngtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
98 thay vì sử dụngtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
99 trong tài liệu hàng đầutasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Mã trong các tài liệu của các chức năng không bao gồm câu lệnh
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
00, nhưng tiếp tục với quy ước
98tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Vấn đề là pytest coi từng tài liệu bằng mã là một bài kiểm tra khác nhau
- Vì tên
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
24, được sử dụng trong trận đấudef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
17 ở tệp cấp cao nhấtdef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
87, sẽ khắc phục sự cố mà không thay đổi mã nguồn- Xem:
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
05 - Bất kỳ tài liệu nào được tìm thấy trong phạm vi của tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
87 này sẽ có ký hiệu
98 được xác địnhtasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Xem:
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...9
Sử dụng def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 225
- Cảnh báo hoạt động rất giống các xác nhận, nhưng được sử dụng cho những thứ không cần ngừng thực thi
- Lịch trình
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
25 được sử dụng để kiểm tra các cảnh báo được tạo bởi mã được thử nghiệm- Xem:
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
10
- Xem:
- Giá trị
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
25 hoạt động giống như một danh sách các cảnh báo và mỗi cảnh báo trong danh sách có$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
12,$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
13,$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
14 và$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
15 được xác định - Các cảnh báo được thu thập khi bắt đầu bài kiểm tra
- Nếu đó là bất tiện vì phần của bài kiểm tra mà bạn quan tâm đến các cảnh báo gần cuối, bạn có thể sử dụng
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
16 để xóa danh sách trước khi kiểm tra mà bạn quan tâm đến việc thu thập cảnh báo
- Nếu đó là bất tiện vì phần của bài kiểm tra mà bạn quan tâm đến các cảnh báo gần cuối, bạn có thể sử dụng
- Pytest cũng có thể kiểm tra cảnh báo với
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
17
def test_add_raises(): """add() should raise an exception with wrong type param.""" with pytest.raises(TypeError): tasks.add(task="not a Task object")0
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
25 và Trình quản lý bối cảnh$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
17 cung cấp chức năng tương tự, vì vậy quyết định sử dụng hoàn toàn là vấn đề của hương vị
5.Plugins
- Cơ sở mã pytest được cấu trúc với tùy chỉnh và tiện ích mở rộng, và có những móc có sẵn để cho phép sửa đổi và cải tiến thông qua các plugin
Tìm plugin
- https://docs.pytest.org/en/latest/plugins.html
- Liệt kê một vài plugin phổ biến
- https://pypi.python.org
- Chỉ số gói Python (PYPI) là một nơi tuyệt vời để tìm plugin pytest
- Nhập "pytest", "pytest-," hoặc "-pytest" vào hộp tìm kiếm
- https://github.com/pytest-dev
- Bạn có thể tìm thấy một số plugin pytest phổ biến được dự định sẽ được duy trì lâu dài bởi nhóm lõi pytest
Cài đặt plugin
Cài đặt từ Pypi
- Vì PYPI là vị trí mặc định cho PIP, việc cài đặt các plugin từ PYPI là phương pháp dễ nhất
def test_add_raises(): """add() should raise an exception with wrong type param.""" with pytest.raises(TypeError): tasks.add(task="not a Task object")1
- Điều này cài đặt phiên bản ổn định mới nhất từ PYPI.
Cài đặt từ .tar.gz hoặc .whl
- Các gói tệp trên PYPI được phân phối dưới dạng các tệp có khóa
- bạn có thể tải xuống và cài đặt từ đó
def test_add_raises(): """add() should raise an exception with wrong type param.""" with pytest.raises(TypeError): tasks.add(task="not a Task object")2
Cài đặt từ một thư mục cục bộ
- Bạn có thể giữ một stash plugin cục bộ (và các gói Python khác) trong thư mục cục bộ hoặc chia sẻ ở định dạng
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
20 hoặc$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
21 và sử dụng nó thay thế
def test_add_raises(): """add() should raise an exception with wrong type param.""" with pytest.raises(TypeError): tasks.add(task="not a Task object")3
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
24 bảo$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
25 không kết nối với PYPI- Is
Cài đặt từ kho lưu trữ Git
def test_add_raises(): """add() should raise an exception with wrong type param.""" with pytest.raises(TypeError): tasks.add(task="not a Task object")4
- Với thẻ phiên bản:
def test_add_raises(): """add() should raise an exception with wrong type param.""" with pytest.raises(TypeError): tasks.add(task="not a Task object")5
- Chỉ định một chi nhánh:
def test_add_raises(): """add() should raise an exception with wrong type param.""" with pytest.raises(TypeError): tasks.add(task="not a Task object")6
Viết các plugin của riêng bạn
- Plugin có thể bao gồm các chức năng hook thay đổi hành vi của pytest
- Rất nhiều chức năng móc có sẵn
- hiểu:
- https://docs.pytest.org/en/latest/_modules/_pytest/hookspec.html
- https://docs.pytest.org/en/latest/reference.html#hooks
- hiểu:
- Thông thường, những thay đổi mà bạn chỉ dự định sử dụng trong một dự án sẽ đủ hữu ích để chia sẻ và phát triển thành một plugin
- Do đó, chúng tôi sẽ bắt đầu bằng cách thêm chức năng vào tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
87, sau đó, sau khi chúng tôi có những thứ hoạt động trong$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
29, chúng tôi sẽ chuyển mã sang gói
- Do đó, chúng tôi sẽ bắt đầu bằng cách thêm chức năng vào tệp
- Xem:
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
22
def test_add_raises(): """add() should raise an exception with wrong type param.""" with pytest.raises(TypeError): tasks.add(task="not a Task object")7
- Thay đổi:
- Xem:
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
88 - Thêm "Cảm ơn vì đã chạy các bài kiểm tra" vào tiêu đề
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
32
- Thay đổi các chỉ số trạng thái thất bại (
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
84) thành "cơ hội cải tiến" ($ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
34)$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
35
- Sử dụng tùy chọn
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
36 để bật hành vi$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
37
- Xem:
def test_add_raises(): """add() should raise an exception with wrong type param.""" with pytest.raises(TypeError): tasks.add(task="not a Task object")8
def test_add_raises(): """add() should raise an exception with wrong type param.""" with pytest.raises(TypeError): tasks.add(task="not a Task object")9
Tạo một plugin có thể cài đặt được
- Cấu trúc thư mục:
def test_start_tasks_db_raises(): """Make sure unsupported db raises an exception.""" with pytest.raises(ValueError) as excinfo: tasks.start_tasks_db("some/great/path", "mysql") exception_msg = excinfo.value.args[0] assert exception_msg == "db_type must be a 'tiny' or 'mongo'"0
- Nhìn thấy:
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
38$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
39$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
40- Một số dạng readme là một yêu cầu của
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
41
- Một số dạng readme là một yêu cầu của
Thử nghiệm plugin
- Kiểm tra một plugin bằng plugin có tên
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
42 được vận chuyển với pytest nhưng bị tắt theo mặc định- Để sử dụng
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
42, chúng ta cần thêm một dòng vàodef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
87 - Một vật cố được gọi là
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
45 sẽ có sẵn khi Pytester được bật - Xem:
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
88
- Để sử dụng
- Thêm "Cảm ơn vì đã chạy các bài kiểm tra" vào tiêu đề
- Thay đổi các chỉ số trạng thái thất bại (
$ pytest ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py F [100%] ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ---------------------- Captured stdout call ---------------------- --- a is 1 ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ======================== $ pytest -s ====================== test session starts ======================= ... collected 1 item tests/test_s_option.py --- a is 1 F ============================ FAILURES ============================ ___________________________ test_fail ____________________________ def test_fail(): a = 1 print("--- a is " + str(a)) > assert a == 2 E assert 1 == 2 tests/test_s_option.py:4: AssertionError ==================== short test summary info ===================== FAILED tests/test_s_option.py::test_fail - assert 1 == 2 ======================= 1 failed in 0.02s ========================
84) thành "cơ hội cải tiến" ($ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
34)
def test_start_tasks_db_raises(): """Make sure unsupported db raises an exception.""" with pytest.raises(ValueError) as excinfo: tasks.start_tasks_db("some/great/path", "mysql") exception_msg = excinfo.value.args[0] assert exception_msg == "db_type must be a 'tiny' or 'mongo'"1
- Sử dụng tùy chọn
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
36 để bật hành vi$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
48
Tạo một plugin có thể cài đặt được
- Cấu trúc thư mục:
def test_start_tasks_db_raises(): """Make sure unsupported db raises an exception.""" with pytest.raises(ValueError) as excinfo: tasks.start_tasks_db("some/great/path", "mysql") exception_msg = excinfo.value.args[0] assert exception_msg == "db_type must be a 'tiny' or 'mongo'"2
- Nhìn thấy:
Một số dạng readme là một yêu cầu của $ cd /path/to/code/ch2/tasks_proj/tests/unit
$ pytest test_task.py
41
- Thử nghiệm plugin
- Kiểm tra một plugin bằng plugin có tên
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
42 được vận chuyển với pytest nhưng bị tắt theo mặc định$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
52
Để sử dụng $ cd /path/to/code/ch2/tasks_proj/tests/unit
$ pytest test_task.py
42, chúng ta cần thêm một dòng vào def test_fail():
a = 1
print("--- a is " + str(a))
assert a == 2
87
- Một vật cố được gọi là
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
45 sẽ có sẵn khi Pytester được bật- Kiểm tra:
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
47
- Kiểm tra:
Để chạy các bài kiểm tra:
Chúng ta có thể gỡ cài đặt nó giống như bất kỳ gói python hoặc plugin pytest nào khác
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
85- Tạo một phân phối
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
87- Chúng tôi có thể sử dụng tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
95 để tạo phân phối
- Chúng tôi có thể sử dụng tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
74- Lưu ý:
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
50 là viết tắt của "phân phối nguồn"
- Lưu ý:
- Nếu bạn sử dụng Tox,
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
58- tương tự như
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
85, nhưng đối với Tox - Bạn có thể đặt cấu hình pytest của mình vào đây thay vì có cả tệp
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
58 vàdef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
85
- tương tự như
- Nếu bạn muốn phân phối gói Python,
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
62- Trong định dạng tệp
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
63 và ảnh hưởng đến hành vi củadef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
95 - Có thể thêm một vài dòng vào
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
95 để cho phép bạn chạy Python$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
66 và để nó chạy tất cả các bài kiểm tra pytest của bạn - Nếu bạn đang phân phối một gói, bạn có thể đã có tệp
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
62 và bạn có thể sử dụng tệp đó để lưu trữ cấu hình pytest
- Trong định dạng tệp
- Bạn có thể nhận danh sách tất cả các cài đặt hợp lệ cho
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
85 từ$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
69 - Có thể cho các plugin (và các tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
87) để thêm các tùy chọn tệp$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
63- Các tùy chọn được thêm vào cũng sẽ được thêm vào đầu ra
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
72 pytest
- Các tùy chọn được thêm vào cũng sẽ được thêm vào đầu ra
Thay đổi các tùy chọn dòng lệnh mặc định
- Để luôn sử dụng một số tùy chọn dòng lệnh cho một dự án, hãy đặt
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
73 trongdef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
85 cho các tùy chọn bạn muốn:
def test_start_tasks_db_raises(): """Make sure unsupported db raises an exception.""" with pytest.raises(ValueError) as excinfo: tasks.start_tasks_db("some/great/path", "mysql") exception_msg = excinfo.value.args[0] assert exception_msg == "db_type must be a 'tiny' or 'mongo'"3
Đăng ký đánh dấu để tránh lỗi chính tả
- Thật quá dễ dàng để đánh dấu một điểm đánh dấu và cuối cùng có một số bài kiểm tra với các điểm đánh dấu khác nhau, ví dụ:
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
75 và$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
76 - Đăng ký đánh dấu trong
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
85- Xem:
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
78 - Nếu bạn sử dụng tùy chọn dòng lệnh
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
79
- Xem:
def test_start_tasks_db_raises(): """Make sure unsupported db raises an exception.""" with pytest.raises(ValueError) as excinfo: tasks.start_tasks_db("some/great/path", "mysql") exception_msg = excinfo.value.args[0] assert exception_msg == "db_type must be a 'tiny' or 'mongo'"4
def test_start_tasks_db_raises(): """Make sure unsupported db raises an exception.""" with pytest.raises(ValueError) as excinfo: tasks.start_tasks_db("some/great/path", "mysql") exception_msg = excinfo.value.args[0] assert exception_msg == "db_type must be a 'tiny' or 'mongo'"5
Yêu cầu phiên bản pytest tối thiểu
- Cài đặt
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
80 cho phép bạn chỉ định phiên bản pytest tối thiểu mà bạn mong đợi cho các bài kiểm tra của mình$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
81 để kiểm tra số điểm nổi cho được đưa vào phiên bản pytest 3.0
def test_start_tasks_db_raises(): """Make sure unsupported db raises an exception.""" with pytest.raises(ValueError) as excinfo: tasks.start_tasks_db("some/great/path", "mysql") exception_msg = excinfo.value.args[0] assert exception_msg == "db_type must be a 'tiny' or 'mongo'"6
Ngừng Pytest nhìn nhầm những nơi
- Thử nghiệm khám phá đi qua nhiều thư mục đệ quy
- Có một số thư mục mà bạn không muốn nhìn vào
- Cài đặt mặc định cho
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
82 là$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
83 và$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
84- Bạn có thể thêm
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
85 vàdef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
71
- Bạn có thể thêm
def test_start_tasks_db_raises(): """Make sure unsupported db raises an exception.""" with pytest.raises(ValueError) as excinfo: tasks.start_tasks_db("some/great/path", "mysql") exception_msg = excinfo.value.args[0] assert exception_msg == "db_type must be a 'tiny' or 'mongo'"7
Chỉ định vị trí thư mục kiểm tra
- Đối diện với
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
82,$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
88 cho Pytest tìm nơi nhìn- Danh sách các thư mục liên quan đến thư mục gốc
- Example:
def test_start_tasks_db_raises(): """Make sure unsupported db raises an exception.""" with pytest.raises(ValueError) as excinfo: tasks.start_tasks_db("some/great/path", "mysql") exception_msg = excinfo.value.args[0] assert exception_msg == "db_type must be a 'tiny' or 'mongo'"8
def test_start_tasks_db_raises(): """Make sure unsupported db raises an exception.""" with pytest.raises(ValueError) as excinfo: tasks.start_tasks_db("some/great/path", "mysql") exception_msg = excinfo.value.args[0] assert exception_msg == "db_type must be a 'tiny' or 'mongo'"9
Thay đổi quy tắc khám phá kiểm tra
- Quy tắc khám phá kiểm tra tiêu chuẩn:
- Bắt đầu ở một hoặc nhiều thư mục
- Bạn có thể chỉ định tên tệp hoặc tên thư mục trên dòng lệnh
- Nếu bạn không chỉ định bất cứ điều gì, thư mục hiện tại sẽ được sử dụng
- Nhìn vào thư mục và tất cả các thư mục con đệ quy cho các mô -đun thử nghiệm
- Mô -đun thử nghiệm là một tệp có tên trông giống như
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
89 hoặc$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
90
- Mô -đun thử nghiệm là một tệp có tên trông giống như
- Xem trong các mô -đun thử nghiệm cho các chức năng bắt đầu bằng
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
91 - Tìm kiếm các lớp bắt đầu bằng
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
92- Tìm các phương thức trong các lớp bắt đầu bằng
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
91 nhưng không có phương thức$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
94
- Tìm các phương thức trong các lớp bắt đầu bằng
- Bắt đầu ở một hoặc nhiều thư mục
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
95,$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
96 và$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
97 Cho phép chúng tôi đặt tên cho các lớp kiểm tra, tệp và chức năng/phương thức tên
@pytest.mark.smoke def test_list_raises(): """One marker: 'smoke'""" ... @pytest.mark.get @pytest.mark.smoke def test_get_raises(): """Two markers: 'get' and 'smoke'""" ...0
Không cho phép xpass
- Cài đặt
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
98 gây ra các bài kiểm tra được đánh dấu bằng$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
99 không được báo cáo là lỗi
Tránh va chạm tên tệp
- Nếu bạn có các tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
74 trống trong tất cả các thư mục kiểm tra của mình, bạn có thể có cùng tên tệp thử nghiệm hiển thị trong nhiều thư mục
7. Sử dụng pytest với các công cụ khác
PDB: Thất bại kiểm tra gỡ lỗi
- Các tùy chọn pytest có sẵn để giúp tăng tốc độ thất bại kiểm tra gỡ lỗi:
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
01: Kiểm soát phong cách theo dõi$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
02: Hiển thị tất cả các tên kiểm tra, vượt qua hoặc không thành công$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
03: Hiển thị các biến cục bộ cùng với StackTrace$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
04: Chỉ chạy các bài kiểm tra thất bại cuối cùng$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
05: Dừng phiên kiểm tra với thất bại đầu tiên$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
06: Bắt đầu một phiên gỡ lỗi tương tác tại điểm thất bại
- Các lệnh mà bạn có thể sử dụng khi bạn đang ở dấu nhắc
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
07:$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
08: In giá trị của$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
09$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
10: in đẹp giá trị của$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
09$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
12: Liệt kê điểm thất bại và năm dòng mã ở trên và bên dưới$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
13: Liệt kê các số dòng cụ thể$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
14: In các đối số của hàm hiện tại với các giá trị của chúng (hữu ích khi trong hàm trợ giúp kiểm tra)$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
15: Di chuyển lên một cấp độ trong dấu vết ngăn xếp$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
16: Di chuyển xuống một cấp độ trong dấu vết ngăn xếp$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
17: Tho bỏ phiên gỡ lỗi- Các lệnh điều hướng khác như
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
18 và$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
19 không hữu ích vì chúng tôi đang ngồi ngay tại một tuyên bốdef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
12 - Bạn cũng chỉ có thể nhập tên biến và nhận các giá trị
@pytest.mark.smoke def test_list_raises(): """One marker: 'smoke'""" ... @pytest.mark.get @pytest.mark.smoke def test_get_raises(): """Two markers: 'get' and 'smoke'""" ...1
FAVEAGE.PY: Xác định số lượng mã được kiểm tra
- Bảo hiểm.py là công cụ bảo hiểm Python ưa thích để đo lường phạm vi bảo hiểm mã
- Cài đặt plugin
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
21 sẽ kéo vào$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
22 vì phạm vi bảo hiểm là một trong những phụ thuộc của nó$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
23
@pytest.mark.smoke def test_list_raises(): """One marker: 'smoke'""" ... @pytest.mark.get @pytest.mark.smoke def test_get_raises(): """Two markers: 'get' and 'smoke'""" ...2
@pytest.mark.smoke def test_list_raises(): """One marker: 'smoke'""" ... @pytest.mark.get @pytest.mark.smoke def test_get_raises(): """Two markers: 'get' and 'smoke'""" ...3
Mock: Trao đổi một phần của hệ thống
- Gói
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
24 được vận chuyển như một phần của Thư viện tiêu chuẩn Python là
90 như của Python 3.3tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Để sử dụng với pytest, một plugin có tên
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
26 có một số tiện ích $ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
27 trong$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
28- phụ thuộc vào một vài chức năng khác
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
29, là người quản lý bối cảnh$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
30, là hàm API trong$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
31
- Sử dụng Mock để đặt các chức năng giả vào vị trí cho
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
29 và$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
33- Chúng ta có thể gọi phương thức
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
34 thông qua giao diện dòng lệnh và đảm bảo nó- gọi chính xác chức năng
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
33 - thỏa thuận với giá trị trả về một cách chính xác
- gọi chính xác chức năng
- Chúng ta có thể gọi phương thức
- phụ thuộc vào một vài chức năng khác
- STUB
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
36- Xem
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
37 trong$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
38
- Xem
- Sử dụng
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
39 để thay thế Trình quản lý bối cảnh thực bằng cuống của chúng tôi$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
39 là một vật cố được cung cấp bởi plugin$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
26 một giao diện tiện lợi cho
90tasks_proj/ ├── CHANGELOG.rst ├── LICENSE ├── MANIFEST.in ├── README.rst ├── setup.py ├── src │ └── tasks │ ├── __init__.py │ ├── api.py │ ├── cli.py │ ├── config.py │ ├── tasksdb_pymongo.py │ └── tasksdb_tinydb.py └── tests ├── conftest.py ├── pytest.ini ├── func │ ├── __init__.py │ ├── test_add.py │ └── ... └── unit ├── __init__.py ├── test_task.py └── ...
- Xem
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
43 trong$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
38
- Lớp
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
45 là một lớp con linh hoạt của$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
46 với hành vi mặc định hợp lý và khả năng chỉ định giá trị trả về - Các lớp
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
47 và$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
45 (và các lớp khác) được sử dụng để bắt chước giao diện của mã khác với các phương thức hướng nội được tích hợp để cho phép bạn hỏi chúng như thế nào chúng được gọi
@pytest.mark.smoke def test_list_raises(): """One marker: 'smoke'""" ... @pytest.mark.get @pytest.mark.smoke def test_get_raises(): """Two markers: 'get' and 'smoke'""" ...4
- Nhìn thấy:
- https://docs.python.org/dev/library/unittest.mock.html
- https://pypi.org/project/pytest-mock/
Tox: Kiểm tra nhiều cấu hình
- TOX là một công cụ dòng lệnh cho phép bạn chạy bộ kiểm tra hoàn chỉnh của mình trong nhiều môi trường
- Bạn có thể sử dụng nó để kiểm tra với các
- Phiên bản của Python
- Cấu hình phụ thuộc
- cấu hình cho các hệ điều hành khác nhau
- Tox sử dụng tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
95 cho gói đang được kiểm tra để tạo phân phối nguồn có thể cài đặt được - Nó trông trong
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
58 cho một danh sách các môi trường và sau đó cho mỗi môi trường, TOX:- Tạo một môi trường ảo trong thư mục
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
51 $ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
25 cài đặt một số phụ thuộc$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
25 Cài đặt gói của bạn từ$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
50- Chạy các bài kiểm tra của bạn
- báo cáo một bản tóm tắt về cách tất cả họ đã làm
- Tạo một môi trường ảo trong thư mục
- Thêm tệp
$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
58 ở cùng cấp độ vớidef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
95 - thư mục dự án hàng đầu- Di chuyển bất cứ thứ gì trong
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
85 vào$ cd /path/to/code/ch2/tasks_proj/tests/unit $ pytest test_task.py
58 - Xem
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
59
- Di chuyển bất cứ thứ gì trong
- Cài đặt TOX (có thể được thực hiện trong môi trường ảo):
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
60
- Chạy Tox:
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
61
- Xem: https://tox.readthedocs.io/en/latest/
A3. Gói lấy mẫu plugin
- Tất cả các plugin đặc trưng ở đây đều có sẵn trên PYPI và được cài đặt với
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
62
Các plugin thay đổi luồng chạy thử thông thường
pytest-lặp lại: chạy thử nghiệm nhiều lần
- Để chạy thử nghiệm nhiều lần mỗi lần, hãy sử dụng plugin
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
63 - Plugin này rất hữu ích nếu bạn bị lỗi không liên tục trong một thử nghiệm
- Bạn có thể sử dụng
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
64 để chạy mọi thứ hai lần - Xem: https://pypi.org/project/pytest-repeat/
@pytest.mark.smoke def test_list_raises(): """One marker: 'smoke'""" ... @pytest.mark.get @pytest.mark.smoke def test_get_raises(): """Two markers: 'get' and 'smoke'""" ...5
@pytest.mark.smoke def test_list_raises(): """One marker: 'smoke'""" ... @pytest.mark.get @pytest.mark.smoke def test_get_raises(): """Two markers: 'get' and 'smoke'""" ...6
pytest-xdist: chạy kiểm tra song song
- Nếu các bài kiểm tra của bạn không cần truy cập vào tài nguyên được chia sẻ, bạn có thể tăng tốc các phiên kiểm tra bằng cách chạy song song nhiều thử nghiệm bằng cách sử dụng plugin
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
65 - Bạn có thể chỉ định nhiều bộ xử lý và chạy nhiều thử nghiệm song song
- Bạn thậm chí có thể đẩy các thử nghiệm lên các máy khác và sử dụng nhiều hơn một máy tính
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
66- Một bài kiểm tra mất ít nhất một giây để chạy, với tham số để nó chạy mười lần
- Xem: https://pypi.org/project/pytest-xdist/
@pytest.mark.smoke def test_list_raises(): """One marker: 'smoke'""" ... @pytest.mark.get @pytest.mark.smoke def test_get_raises(): """Two markers: 'get' and 'smoke'""" ...7
@pytest.mark.smoke def test_list_raises(): """One marker: 'smoke'""" ... @pytest.mark.get @pytest.mark.smoke def test_get_raises(): """Two markers: 'get' and 'smoke'""" ...8
- Bạn có thể dùng
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
67 để chạy mỗi bài kiểm tra trong một quy trình con$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
68 để tự động phát hiện số lượng CPU trên hệ thống
@pytest.mark.smoke def test_list_raises(): """One marker: 'smoke'""" ... @pytest.mark.get @pytest.mark.smoke def test_get_raises(): """Two markers: 'get' and 'smoke'""" ...9
Pytest Timeout: Đặt giới hạn thời gian cho các bài kiểm tra của bạn
- Plugin
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
69 cho phép bạn vượt qua khoảng thời gian chờ trên dòng lệnh hoặc đánh dấu các thử nghiệm riêng lẻ với thời gian chờ tính bằng giây- Dấu ghi đè lên thời gian chờ dòng lệnh
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
70
- Dấu ghi đè lên thời gian chờ dòng lệnh
- Xem: https://pypi.org/project/pytest-tineout/
$ pytest -v -m 'smoke or get' test_api_exceptions.py ============================= test session starts ============================== ... collected 7 items / 5 deselected / 2 selected test_api_exceptions.py::test_list_raises PASSED [ 50%] test_api_exceptions.py::test_get_raises PASSED [100%]0
$ pytest -v -m 'smoke or get' test_api_exceptions.py ============================= test session starts ============================== ... collected 7 items / 5 deselected / 2 selected test_api_exceptions.py::test_list_raises PASSED [ 50%] test_api_exceptions.py::test_get_raises PASSED [100%]1
Các plugin thay đổi hoặc tăng cường đầu ra
- Các plugin này không thay đổi cách kiểm tra được chạy, nhưng chúng thay đổi đầu ra mà bạn thấy
pytest-instafail: xem chi tiết về thất bại và lỗi khi chúng xảy ra
- Nếu bộ thử nghiệm của bạn mất khá nhiều thời gian, bạn có thể muốn xem các dấu vết khi chúng xảy ra, thay vì đợi cho đến khi kết thúc
- Xem: https://pypi.org/project/pytest-instafail/
$ pytest -v -m 'smoke or get' test_api_exceptions.py ============================= test session starts ============================== ... collected 7 items / 5 deselected / 2 selected test_api_exceptions.py::test_list_raises PASSED [ 50%] test_api_exceptions.py::test_get_raises PASSED [100%]2
$ pytest -v -m 'smoke or get' test_api_exceptions.py ============================= test session starts ============================== ... collected 7 items / 5 deselected / 2 selected test_api_exceptions.py::test_list_raises PASSED [ 50%] test_api_exceptions.py::test_get_raises PASSED [100%]3
Pytest-Sugar: Instafail + Colors + Progress
- Cho phép bạn xem trạng thái không chỉ là ký tự, mà còn về màu sắc
- Cũng hiển thị các dấu vết lỗi và lỗi trong quá trình thực hiện và có một thanh tiến trình mát mẻ ở bên phải của vỏ
- Xem: https://pypi.org/project/pytest-sugar/
$ pytest -v -m 'smoke or get' test_api_exceptions.py ============================= test session starts ============================== ... collected 7 items / 5 deselected / 2 selected test_api_exceptions.py::test_list_raises PASSED [ 50%] test_api_exceptions.py::test_get_raises PASSED [100%]4
$ pytest -v -m 'smoke or get' test_api_exceptions.py ============================= test session starts ============================== ... collected 7 items / 5 deselected / 2 selected test_api_exceptions.py::test_list_raises PASSED [ 50%] test_api_exceptions.py::test_get_raises PASSED [100%]5
$ pytest -v -m 'smoke or get' test_api_exceptions.py ============================= test session starts ============================== ... collected 7 items / 5 deselected / 2 selected test_api_exceptions.py::test_list_raises PASSED [ 50%] test_api_exceptions.py::test_get_raises PASSED [100%]6
Pytest-Emoji: Thêm một số niềm vui vào các bài kiểm tra của bạn
- Cho phép bạn thay thế tất cả các ký tự trạng thái thử nghiệm bằng biểu tượng cảm xúc
- Một plugin nhỏ và là một ví dụ tốt để dựa trên các plugin của riêng bạn
- Cho phép bạn thay đổi biểu tượng cảm xúc bằng cách sử dụng các hàm hook
- Một trong số ít các plugin pytest trình bày cách thêm các hàm hook vào mã plugin
- Xem: https://pypi.org/project/pytest-emoji/
Pytest-HTML: Tạo báo cáo HTML cho các phiên kiểm tra
- Hữu ích khi kết hợp với tích hợp liên tục hoặc trong các hệ thống có bộ thử nghiệm lớn, chạy dài
- Tạo một trang web để xem kết quả kiểm tra cho phiên pytest
- Xem: https://pypi.org/project/pytest-html/
$ pytest -v -m 'smoke or get' test_api_exceptions.py ============================= test session starts ============================== ... collected 7 items / 5 deselected / 2 selected test_api_exceptions.py::test_list_raises PASSED [ 50%] test_api_exceptions.py::test_get_raises PASSED [100%]7
$ pytest -v -m 'smoke or get' test_api_exceptions.py ============================= test session starts ============================== ... collected 7 items / 5 deselected / 2 selected test_api_exceptions.py::test_list_raises PASSED [ 50%] test_api_exceptions.py::test_get_raises PASSED [100%]8
Plugin để phân tích tĩnh
pytest-pycodestyle, pytest-pep8: tuân thủ hướng dẫn phong cách của Python
- Sử dụng plugin
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
71 để chạy$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
72 trên mã trong dự án của bạn, bao gồm mã kiểm tra, với cờ$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
73
pytest-flake8: Kiểm tra phong cách cộng với Lining
- Với plugin
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
74, bạn có thể chạy tất cả mã nguồn và mã kiểm tra thông qua Flake8 và bị lỗi nếu có gì đó không đúng- Kiểm tra PEP 8, cũng như các lỗi logic
- Sử dụng tùy chọn
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
75 để chạy$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
76 trong phiên pytest
- Bạn có thể mở rộng Flake8 với các plugin cung cấp nhiều kiểm tra hơn, chẳng hạn như
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
77
Plugin để phát triển web
Pytest-Selenium: Kiểm tra với trình duyệt web
- Plugin
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
78 là liên kết python cho selen - Với nó, bạn có thể
- Khởi chạy trình duyệt web và sử dụng nó để mở URLS
- tập thể dục các ứng dụng web
- Điền vào các biểu mẫu
- Kiểm soát chương trình trình duyệt để kiểm tra trang web hoặc ứng dụng web
Pytest-Django: Ứng dụng Django kiểm tra
- Theo mặc định, hỗ trợ thử nghiệm tích hợp trong Django dựa trên sự nhất quán
- Plugin
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
79 cho phép bạn sử dụng pytest thay vì không nhất- Bao gồm các chức năng và đồ đạc của người trợ giúp để tăng tốc độ triển khai thử nghiệm
Pytest-Flask: Ứng dụng bình kiểm tra
- Plugin
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
80 cung cấp một số ít đồ đạc để hỗ trợ thử nghiệm các ứng dụng bình
A4. Bao bì và phân phối các dự án Python
Tạo một mô -đun có thể cài đặt
- Đối với một dự án một mô-đun đơn giản, cấu hình tối thiểu là nhỏ
$ pytest -v -m 'smoke or get' test_api_exceptions.py ============================= test session starts ============================== ... collected 7 items / 5 deselected / 2 selected test_api_exceptions.py::test_list_raises PASSED [ 50%] test_api_exceptions.py::test_get_raises PASSED [100%]9
- Mã chúng tôi muốn chia sẻ là trong
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
81 - Để tạo nó có thể cài đặt với PIP, chúng tôi cần một tệp
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
95
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 200
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 201
Tạo một gói có thể cài đặt
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 202
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
74 cần được viết để lộ chức năng mô -đun với thế giới bên ngoài thông qua không gian tên gói- Xem: https://docs.python.org/3/tutorial/modules.html#packages
- Nếu chúng ta làm điều gì đó như thế này trong
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
74:
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 203
- Mã máy khách sẽ phải chỉ định:
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 204
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
85- phơi bày mọi thứ trong đó đến mức gói
- Mã khách hàng có thể làm điều này:
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 205
$ pytest test_task_fail.py ============================= test session starts ============================== platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: /.../ch2/tasks_proj/tests, inifile: pytest.ini collected 2 items test_task_fail.py FF [100%] =================================== FAILURES =================================== ______________________________ test_task_equality ______________________________ def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') > assert t1 == t2 E AssertionError: assert Task(summary=...alse, id=None) == Task(summary=...alse, id=None) E At index 0 diff: 'sit there' != 'do something' E Use -v to get the full diff test_task_fail.py:9: AssertionError ______________________________ test_dict_equality ______________________________ ...
86- Chỉ định các gói
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 206
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 207
- Bạn có thể thêm thư mục
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
70 ở cùng cấpdef test_fail(): a = 1 print("--- a is " + str(a)) assert a == 2
71 để thêm các bài kiểm tra của chúng tôi
Tạo phân phối nguồn và bánh xe
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 208
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 209
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 210
def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 211
Nguồn
- Okken, Brian. Thử nghiệm Python với pytest: đơn giản, nhanh chóng, hiệu quả và có thể mở rộng. Bookshelf thực dụng, 2017.