Hướng dẫn python testing with pytest github - thử nghiệm python với pytest github

Thử nghiệm Python với Pytest

  • Thử nghiệm Python với Pytest
    • 1. Bắt đầu với pytest
      • Sử dụng các tùy chọn
    • 2. Viết chức năng kiểm tra
      • Kiểm tra một gói
        • Cài đặt gói cục bộ
      • Sử dụng các câu lệnh
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        12
      • Mong đợi ngoại lệ
      • Đánh dấu các chức năng kiểm tra
      • Bỏ qua bài kiểm tra
      • Đánh dấu các bài kiểm tra như mong đợi sẽ thất bại
      • Chạy một tập hợp các bài kiểm tra
        • Một thư mục duy nhất
        • Một tệp/mô -đun kiểm tra duy nhất
        • Một chức năng kiểm tra duy nhất
        • Một lớp kiểm tra duy nhất
        • Một phương pháp kiểm tra duy nhất của một lớp kiểm tra
        • Một tập hợp các bài kiểm tra dựa trên tên thử nghiệm
      • Kiểm tra tham số
        • Một tham số
        • Nhiều tham số
        • Đối số được gán cho một biến
        • 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
        • def test_fail():
              a = 1
              print("--- a is " + str(a))
              assert a == 2
          14 trên các lớp học
        • Xác định các tham số với
          def test_fail():
              a = 1
              print("--- a is " + str(a))
              assert a == 2
          15
    • 3. Đồ đạc pytest
      • Chia sẻ đồ đạc thông qua conftest.py
      • Sử dụng đồ đạc để thiết lập và phá vỡ
      • Truy tìm thực hiện vật cố với
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        16
      • Sử dụng đồ đạc cho dữ liệu thử nghiệm
      • Sử dụng nhiều đồ đạc
      • Chỉ định phạm vi cố định
      • Chỉ định đồ đạc với usedfixtures
      • Sử dụng
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        17 cho đồ đạc luôn được sử dụng
      • Đổi tên đồ đạc
      • Đồ đạc tham số
    • 4. Đồ đạc tích hợp
      • Sử dụng
        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
      • Sử dụng
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        20
      • Sử dụng
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        21
      • Sử dụng
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        22
      • Sử dụng
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        23
      • Sử dụng
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        24
      • Sử dụng
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        25
    • 5.Plugins
      • Tìm plugin
      • Cài đặt plugin
        • Cài đặt từ Pypi
        • Cài đặt từ .tar.gz hoặc .whl
        • Cài đặt từ một thư mục cục bộ
        • Cài đặt từ kho lưu trữ Git
      • Viết các plugin của riêng bạn
      • Tạo một plugin có thể cài đặt được
      • Thử nghiệm plugin
      • Tạo một phân phối
        • Phân phối các plugin thông qua một thư mục được chia sẻ
        • Phân phối các plugin thông qua PYPI
    • 6. Cấu hình
      • Hiểu các tệp cấu hình pytest
      • Thay đổi các tùy chọn dòng lệnh mặc định
      • Đăng ký đánh dấu để tránh lỗi chính tả
      • Yêu cầu phiên bản pytest tối thiểu
      • Ngừng Pytest nhìn nhầm những nơi
      • Chỉ định vị trí thư mục kiểm tra
      • Thay đổi quy tắc khám phá kiểm tra
      • Không cho phép xpass
      • Tránh va chạm tên tệp
    • 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
      • FAVEAGE.PY: Xác định số lượng mã được kiểm tra
      • Mock: Trao đổi một phần của hệ thống
      • Tox: Kiểm tra nhiều cấu hình
    • A3. Gói lấy mẫu plugin
      • 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
        • pytest-xdist: chạy kiểm tra song song
        • Pytest Timeout: Đặt giới hạn thời gian cho các bài kiểm tra của bạn
      • Các plugin thay đổi hoặc tăng cường đầu ra
        • pytest-instafail: xem chi tiết về thất bại và lỗi khi chúng xảy ra
        • Pytest-Sugar: Instafail + Colors + Progress
        • Pytest-Emoji: Thêm một số niềm vui vào các bài kiểm tra của bạn
        • Pytest-HTML: Tạo báo cáo HTML cho các phiên kiểm tra
      • Plugin để phân tích tĩnh
        • pytest-pycodestyle, pytest-pep8: tuân thủ hướng dẫn phong cách của Python
        • pytest-flake8: Kiểm tra phong cách cộng với Lining
      • Plugin để phát triển web
        • Pytest-Selenium: Kiểm tra với trình duyệt web
        • Pytest-Django: Ứng dụng Django kiểm tra
        • Pytest-Flask: Ứng dụng bình kiểm tra
    • A4. Bao bì và phân phối các dự án Python
      • Tạo một mô -đun có thể cài đặt
      • Tạo một gói có thể cài đặt
      • Tạo phân phối nguồn và bánh xe
    • Nguồn

1. Bắt đầu với pytest

​$ pytest --help​
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
...

Show
  • Không có đối số nào, Pytest nhìn vào thư mục hiện tại của bạn và tất cả các thư mục con cho các tệp kiểm tra và chạy mã kiểm tra mà nó tìm thấy
  • Nếu bạn đặt cho pytest một tên tệp, tên thư mục hoặc danh sách các tên gọi đó, nó sẽ nhìn ở đó thay vì thư mục hiện tại
  • Mỗi thư mục được liệt kê trên dòng lệnh được đệ quy để tìm mã kiểm tra
  • Phần thực thi pytest trong đó pytest đi ra và tìm thấy những bài kiểm tra nào được gọi là khám phá thử nghiệm
  • Đặt tên quy ước để giữ cho mã kiểm tra của bạn có thể khám phá được:
    • Tệp kiểm tra:
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      26 hoặc
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      27
    • Phương pháp và chức năng kiểm tra:
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      28
    • Các lớp kiểm tra:
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      29
  • Có nhiều cách để thay đổi các quy tắc khám phá này
    • Xem: Cấu hình
  • Chỉ chạy một bài kiểm tra:
    • def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      30
    • ví dụ:
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      31

Sử dụng các tùy chọn

  • Một số tùy chọn dòng lệnh pytest hữu ích
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    32
    • Hiển thị cho bạn những bài kiểm tra nào sẽ được chạy với các tùy chọn và cấu hình đã cho
    • ví dụ: khi được sử dụng cùng với
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      33
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    33
    • Sử dụng biểu thức để tìm những chức năng kiểm tra nào để chạy
    • ví dụ:
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      35
      • Chạy các bài kiểm tra
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        36 và
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        37
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    38
    • Các điểm đánh dấu cho phép bạn đánh dấu một tập hợp các chức năng kiểm tra của bạn để chúng có thể được chạy cùng nhau
    • ví dụ:
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      39
      • Sẽ chạy các bài kiểm tra với điểm đánh dấu
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        40
    • Ví dụ thêm:
      • def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        41
      • def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        42
      • def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        43
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    44
    • Dừng toàn bộ phiên kiểm tra ngay lập tức khi bài kiểm tra không thành công
    • hữu ích khi gỡ lỗi một vấn đề
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    45 và
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    46
    • Cờ
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      45 cho phép mọi thứ thông thường sẽ được in ra stdout để thực sự được in ra stdout trong khi các bài kiểm tra đang chạy
      • phím tắt cho
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        48
    • Ví dụ: Không có và với
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      45:

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 ========================

  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    50,
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    51
    • Khi một hoặc nhiều bài kiểm tra thất bại, chỉ chạy các bài kiểm tra thất bại
    • Hữu ích cho việc gỡ lỗi
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    52,
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    53
    • Chạy tất cả các bài kiểm tra nhưng chạy những thất bại cuối cùng trước
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    54,
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    55
    • báo cáo nhiều thông tin hơn là không có nó
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    56,
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    57
    • Đối diện với ________ 154/________ 155
    • Giảm thông tin được báo cáo
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    60,
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    61
    • Các biến cục bộ và các giá trị của chúng được hiển thị với TraceBacks cho các bài kiểm tra thất bại
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    62
    • Sửa đổi cách đầu ra của Tracerbacks cho các lỗi
    • Phong cách hữu ích:
      • def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        63: Chỉ in dòng
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        12 và dòng
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        65 được đánh giá không có ngữ cảnh
      • def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        66: Giữ sự thất bại của một dòng
      • def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        67: Xóa hoàn toàn dấu vết
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    68
    • Báo cáo số N số thử nghiệm/thiết lập/giọt nước chậm nhất sau khi các bài kiểm tra chạy
    • def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      69 báo cáo mọi thứ theo thứ tự chậm nhất đến nhanh nhất

2. Viết chức năng kiểm tra

Kiểm tra một gói

  • Cấu trúc tệp cho dự án nhiệm 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
​        └── ...

  • Tất cả các thử nghiệm được giữ trong
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    70 và tách biệt với các tệp nguồn gói trong
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    71
    • không phải là một yêu cầu của pytest, mà là một thực hành tốt nhất
  • Các thử nghiệm chức năng (
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    72) và đơn vị (
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    73) được tách thành các thư mục riêng của họ
    • cho phép bạn dễ dàng chạy một tập hợp con của các bài kiểm tra
      • Các bài kiểm tra chức năng chỉ nên phá vỡ nếu chúng ta cố tình thay đổi chức năng của hệ thống
      • Các bài kiểm tra đơn vị có thể bị phá vỡ trong quá trình tái cấu trúc hoặc thay đổi thực hiện
  • Hai loại tệp
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    74
    • theo thư mục
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      75
      • def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        76 nói với Python rằng thư mục là một gói
      • hoạt động như giao diện chính cho gói khi ai đó sử dụng
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        77
      • Chứa mã để nhập các chức năng cụ thể từ
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        78
        • def test_fail():
              a = 1
              print("--- a is " + str(a))
              assert a == 2
          79 và các tệp thử nghiệm của chúng tôi có thể truy cập chức năng gói như
          def test_fail():
              a = 1
              print("--- a is " + str(a))
              assert a == 2
          80 thay vì phải thực hiện
          def test_fail():
              a = 1
              print("--- a is " + str(a))
              assert a == 2
          81
    • theo
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      82
      • Các tệp
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        83 và
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        84 trống
        • Nói với pytest đi lên một thư mục để tìm gốc của thư mục thử nghiệm và tìm tệp
          def test_fail():
              a = 1
              print("--- a is " + str(a))
              assert a == 2
          85
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    85
    • không bắt buộc
    • chứa cấu hình pytest toàn dự án, nhiều nhất chỉ một trong số này trong dự án của bạn
      • chỉ thị thay đổi hành vi của pytest
      • ví dụ: danh sách các tùy chọn sẽ luôn được sử dụng
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    87
    • không bắt buộc
    • chứa cấu hình pytest toàn dự án, nhiều nhất chỉ một trong số này trong dự án của bạn
    • chỉ thị thay đổi hành vi của pytest
      • ví dụ: danh sách các tùy chọn sẽ luôn được sử dụng
    • Được coi là pytest là "plugin cục bộ" và có thể chứa các chức năng và đồ đạc của móc
      • Các chức năng móc
      • Một cách để chèn mã vào một phần của quy trình thực thi pytest để thay đổi cách thức hoạt động của pytest
    • đồ đạc
    • Các chức năng thiết lập và xé rách chạy trước và sau các chức năng kiểm tra
      • có thể được sử dụng để thể hiện tài nguyên và dữ liệu được sử dụng bởi các bài kiểm tra

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
def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
88

  • có thể có nhiều tệp
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    87
    • ví dụ: bạn có thể có một ở
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      70 và một cho mỗi thư mục con theo
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      70
  • Cài đặt gói cục bộ
    • Cách tốt nhất để cho phép các bài kiểm tra có thể
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      77 hoặc
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      93 là cài đặt
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      94 cục bộ bằng PIP
    • có thể bởi vì có một tệp
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      95 có mặt để trực tiếp PIP
      • Cài đặt
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        94 bằng cách chạy
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        97 hoặc
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        98 từ thư mục
        def test_fail():
            a = 1
            print("--- a is " + str(a))
            assert a == 2
        99
  • Hoặc bạn có thể 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 ========================
    00 từ một thư mục trở lên

​$ cd /path/to/code/ch2/tasks_proj/tests/unit​
$ pytest 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 ========================01: Cài đặt một dự án ở chế độ có thể chỉnh sửa (nghĩa là setuptools "phát triển chế độ") từ đường dẫn dự án cục bộ hoặc URL VCS

  • Có thể sửa đổi mã nguồn trong khi
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    94 được cài đặt
  • Chạy các bài kiểm tra:
Sử dụng các câu lệnh
def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
12
Tuyên bố Python
def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
12 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 == 2
12 và các hàm trợ giúp
def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
12:
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

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

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

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
0

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
1

  • $ 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 == 2
2

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
3

  • $ 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

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
4

Đá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 == 2
5

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
6

  • 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:

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
7

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 == 2
8

  • 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

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
9

  • Để 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

$ 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 cho
    def 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
​        └── ...
0

def 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
​        └── ...
1

​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
​        └── ...
2

Xá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ố

​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
​        └── ...
3

​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
​        └── ...
4

3. Đồ đạ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 tra
      def 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
  • 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ẹp
    def 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
​        └── ...
7

​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
​        └── ...
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
​        └── ...
9

Sử 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.py​
0

  • $ 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.py​
1

​$ cd /path/to/code/ch2/tasks_proj/tests/unit​
$ pytest test_task.py​
2

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.py​
3

  • $ 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.py​
1

​$ cd /path/to/code/ch2/tasks_proj/tests/unit​
$ pytest test_task.py​
5

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:
    ​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
    ​        └── ...
    
    00

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
    ​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
    ​        └── ...
    
    02 để có được một trận đấu để chạy mọi lúc
  • Xem:
    ​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
    ​        └── ...
    
    03

Đổi tên đồ đạc

  • pytest cho phép bạn đổi tên đồ đạc bằng tham số
    ​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
    ​        └── ...
    
    04 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 ========================
    69
    ​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
    ​        └── ...
    
    04
    parameter to
    $ 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.py​
6

​$ cd /path/to/code/ch2/tasks_proj/tests/unit​
$ pytest test_task.py​
7

  • Sử dụng tùy chọn pytest
    ​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
    ​        └── ...
    
    06 để tìm ra nơi
    ​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
    ​        └── ...
    
    07 được xác định
    • 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.py​
8

​$ cd /path/to/code/ch2/tasks_proj/tests/unit​
$ pytest test_task.py​
9

Đồ đạc tham số

  • ​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
    ​        └── ...
    
    08

$ 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

  • ​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
    ​        └── ...
    
    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ố
    • Có một trường
      ​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
      ​        └── ...
      
      10 được điền vào một phần tử từ danh sách được gán cho
      ​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
      ​        └── ...
      
      11 trong
      ​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
      ​        └── ...
      
      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 ______________________________
...
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
    ​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
    ​        └── ...
    
    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ượng
    ​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
    ​        └── ...
    
    15, cho phép chúng tôi sử dụng các phương thức truy cậ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
    ​        └── ...
    
    18 để truy cập một đối tượng
    ​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
    ​        └── ...
    
    15 để tạo định danh cho một đối tượng
    ​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
    ​        └── ...
    
    15 tại một thời điểm

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
      ​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
      ​        └── ...
      
      27
  • 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
    • ​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
      ​        └── ...
      
      29: Tạo một thư mụ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
      ​        └── ...
      
      30: Trả về thư mục cơ sở được sử dụng cho phiên này
      • 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
      • ​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
        ​        └── ...
        
        31 để chỉ định thư mục cơ sở của riêng bạn
  • Xem:
    ​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
    ​        └── ...
    
    32
  • 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
      ​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
      ​        └── ...
      
      34 trong chính mô -đun hoặc trong tệp
      def test_fail():
          a = 1
          print("--- a is " + str(a))
          assert a == 2
      87
    • hiể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
        ​        └── ...
        
        36
      • ​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
        ​        └── ...
        
        37

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
    ​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
    ​        └── ...
    
    40
  • Đô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:
        ​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
        ​        └── ...
        
        43

$ 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:
      ​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
      ​        └── ...
      
      45

$ 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
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    21 được sử dụng cho chức năng
    def 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:
    • ​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
      ​        └── ...
      
      52
    • ​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
      ​        └── ...
      
      53
  • 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à
    ​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
    ​        └── ...
    
    54 và tiếp tục phân tách các phần của tên khóa với
    ​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
    ​        └── ...
    
    54S
    • 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:
      ​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
      ​        └── ...
      
      56
    • Xem thê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
      ​        └── ...
      
      57

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
      ​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
      ​        └── ...
      
      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ã
    • Tạm thời vô hiệu hóa việc chụp đầu ra
  • ​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
    ​        └── ...
    
    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
    ​        └── ...
    
    64
    • 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
      ​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
      ​        └── ...
      
      65
  • Sử dụng
    ​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
    ​        └── ...
    
    66 để tạm thời cho đầu ra vượt qua cơ chế bắt giữ

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:
    • ​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
      ​        └── ...
      
      69: Đặt thuộc tính
    • ​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
      ​        └── ...
      
      70: Xóa thuộc tính
    • ​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
      ​        └── ...
      
      71: Đặt mục từ điển
    • ​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
      ​        └── ...
      
      72: Xóa mục từ điển
    • ​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
      ​        └── ...
      
      73: Đặt một biến môi trường
    • ​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
      ​        └── ...
      
      74: Xóa biến môi trường
    • ​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
      ​        └── ...
      
      75: Đường dẫn dành cho Sys.Path, đó là danh sách các vị trí nhập khẩu của Python
    • ​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
      ​        └── ...
      
      76: Thay đổi thư mục làm việc hiện tại
  • Xem: https://docs.pytest.org/en/latest/reference.html#monkeypatch
  • Xem xét (xem:
    ​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
    ​        └── ...
    
    77):

$ 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

  • ​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
    ​        └── ...
    
    78 là một hàm không có tham số và không trả về bất cứ điều gì
    • 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á
      ​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
      ​        └── ...
      
      79 để mọi thứ trong mô -đun
      ​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
      ​        └── ...
      
      80 gọi
      ​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
      ​        └── ...
      
      81 có biểu thức Lambda của chúng tôi thay thế
    • Xem:
      ​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
      ​        └── ...
      
      82 trong
      ​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
      ​        └── ...
      
      83

$ 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
    ​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
    ​        └── ...
    
    84 để thay đổi các mục từ điển chỉ trong suốt thời gian thử nghiệm
    • Xem:
      ​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
      ​        └── ...
      
      85 trong
      ​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
      ​        └── ...
      
      83
  • ​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
    ​        └── ...
    
    75
    • Đặ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
  • ​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
    ​        └── ...
    
    76
    • 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
    ​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
    ​        └── ...
    
    90 để thay thế các thuộc tính bằng các đối tượng giả

Sử dụng def test_fail(): a = 1 print("--- a is " + str(a)) assert a == 224

  • Mô -đun
    ​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
    ​        └── ...
    
    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 động
  • 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ờ
    ​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
    ​        └── ...
    
    93
  • 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 đồ đạc
    def 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:
    ​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
    ​        └── ...
    
    96
    • Vì tên
      ​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
      ​        └── ...
      
      97 dài, chúng tôi quyết định sử dụng
      ​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
      ​        └── ...
      
      98 thay vì sử dụng
      ​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
      ​        └── ...
      
      99 trong tài liệu hàng đầu
    • 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
      ​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
      ​        └── ...
      
      98
    • 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
  • def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    24, được sử dụng trong trận đấu
    def test_fail():
        a = 1
        print("--- a is " + str(a))
        assert a == 2
    17 ở tệp cấp cao nhất
    def 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
      ​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
      ​        └── ...
      
      98 được xá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 ______________________________
...
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
  • 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
  • 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
  • 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
  • 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

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

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à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
    • 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 đề
  • 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

Để 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
  • 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"
  • 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
  • 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ủa
      def 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
  • 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

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 trong
    def 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

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

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
    • 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
  • ​$ 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à
    ​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
    ​        └── ...
    
    90 như của Python 3.3
  • Để 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
  • 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
  • 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
      ​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
      ​        └── ...
      
      90
    • 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
  • Thêm tệp
    ​$ cd /path/to/code/ch2/tasks_proj/tests/unit​
    $ pytest test_task.py​
    58 ở cùng cấp độ với
    def 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
  • 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
  • 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 == 2
00

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
01

Tạo một gói có thể cài đặt

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
02

  • 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 == 2
03

  • Mã máy khách sẽ phải chỉ định:

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
04

  • $ 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 == 2
05

  • $ 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 == 2
06

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
07

  • 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ấp
    def 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 == 2
08

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
09

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
10

def test_fail():
    a = 1
    print("--- a is " + str(a))
    assert a == 2
11

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.

Tại sao pytest tốt hơn một cách nhất quán?

Unittest yêu cầu các nhà phát triển tạo các lớp có nguồn gốc từ mô -đun TestCase và sau đó xác định các trường hợp thử nghiệm là phương thức trong lớp.Mặt khác, pytest chỉ yêu cầu bạn xác định một chức năng với Test Test_ Hồi được chuẩn bị và sử dụng các điều kiện khẳng định bên trong chúng.only requires you to define a function with “test_” prepended and use the assert conditions inside them.

Pytest có phải là một công cụ thử nghiệm không?

PyTest là một khung kiểm tra phần mềm, có nghĩa là PyTest là một công cụ dòng lệnh tự động tìm thấy các bài kiểm tra bạn đã viết, chạy các bài kiểm tra và báo cáo kết quả., which means pytest is a command-line tool that automatically finds tests you've written, runs the tests, and reports the results.

Pytest có phải là TDD không?

Phát triển theo hướng thử nghiệm (TDD) là một thực tiễn phát triển phần mềm yêu cầu chúng tôi viết các bài kiểm tra tăng dần cho các tính năng mà chúng tôi muốn thêm.Nó tận dụng các bộ thử nghiệm tự động, như pytest - một khung thử nghiệm cho các chương trình Python.a testing framework for Python programs.

Pytest có phải là người chạy thử không?

Khi gói pytest được cài đặt, Pycharm phát hiện nó và làm cho nó trở thành người chạy thử nghiệm dự án mặc định..