Python unittest khẳng định dict bằng

Thư viện Pytest cung cấp một cách tốt hơn để viết bài kiểm tra, chạy kiểm tra và báo cáo kết quả kiểm tra. Bài đăng này là sự so sánh giữa thư viện tiêu chuẩn kiểm tra đơn vị Python và các tính năng pytest và loại bỏ các thư viện khác như nose2

TL;DR

  • Câu lệnh khẳng định đơn (
    def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    6) trên 40 phương thức khẳng định khác nhau (
    def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    7)
  • Thông báo lỗi chi tiết và tốt hơn khi thất bại
  • Các tùy chọn dòng lệnh hữu ích để báo cáo, khám phá và báo cáo các bài kiểm tra như
    def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    8,
    def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    9
  • Các plugin pytest để mở rộng các chức năng pytest và sửa đổi hành vi mặc định.
    import unittest
    
    from dataclasses import dataclass
    
    @dataclass
    class Book:
        name: str
        year: int
        author: str
    
    
    class TestUnitTestShowCase(unittest.TestCase):
        def test_assert_equal(self):
            expected_json = {'name': 'Haruki Murakami',
                             'language': 'Japanese',
                             'title': 'Windup Bird Chronicle',
                             'year_of_release': 1994,
                             'page_count': 607,
                             'genres': ['Novel', 'Science Fiction',
                                        'Pyschological Fiction']}
            return_json = {'name': 'Haruki Murakami',
                           'language': 'Japanese',
                           'title': 'Kafka on the shore',
                           'year_of_release': 2002,
                           'page_count': 505,
                           'genres': ['Novel', 'Magical Realism',
                                      'Fantasy Fiction']}
    
            self.assertDictEqual(return_json, expected_json)
    
        def test_dataclass(self):
            windup = Book(name='Windup Bird Chronicle', year=1994,
                          author='Haruki Murakami')
            kafka = Book(name='Kafka on the shore', year=2002,
                         author='Haruki Murakami')
    
            self.assertEqual(windup, kafka)
    
    
    if __name__ == "__main__":
        unittest.main()
    
    0
  • Đồ đạc Pytest cho dữ liệu hạt giống và triển khai các hành vi kiểm tra tùy chỉnh
1. Câu lệnh xác nhận duy nhất trên 40 phương thức xác nhận khác nhau

Đây là một mã đơn vị mẫu

import unittest


class TestUnitTestShowCase(unittest.TestCase):
		def test_equal(self):
				v1 = "start"
				v2 = "start+"

				self.assertEqual(v1, v2)

    def test_dictionary(self):
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}

        self.assertDictEqual(rust, python)

    def test_list(self):
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']

        self.assertListEqual(expected_genres, returned_genres)

Lớp

import unittest

from dataclasses import dataclass

@dataclass
class Book:
    name: str
    year: int
    author: str


class TestUnitTestShowCase(unittest.TestCase):
    def test_assert_equal(self):
        expected_json = {'name': 'Haruki Murakami',
                         'language': 'Japanese',
                         'title': 'Windup Bird Chronicle',
                         'year_of_release': 1994,
                         'page_count': 607,
                         'genres': ['Novel', 'Science Fiction',
                                    'Pyschological Fiction']}
        return_json = {'name': 'Haruki Murakami',
                       'language': 'Japanese',
                       'title': 'Kafka on the shore',
                       'year_of_release': 2002,
                       'page_count': 505,
                       'genres': ['Novel', 'Magical Realism',
                                  'Fantasy Fiction']}

        self.assertDictEqual(return_json, expected_json)

    def test_dataclass(self):
        windup = Book(name='Windup Bird Chronicle', year=1994,
                      author='Haruki Murakami')
        kafka = Book(name='Kafka on the shore', year=2002,
                     author='Haruki Murakami')

        self.assertEqual(windup, kafka)


if __name__ == "__main__":
    unittest.main()
1 hỗ trợ 40 phương thức xác nhận khác nhau (https. // tài liệu. con trăn. org/3/library/unittest. html). Phương pháp
import unittest

from dataclasses import dataclass

@dataclass
class Book:
    name: str
    year: int
    author: str


class TestUnitTestShowCase(unittest.TestCase):
    def test_assert_equal(self):
        expected_json = {'name': 'Haruki Murakami',
                         'language': 'Japanese',
                         'title': 'Windup Bird Chronicle',
                         'year_of_release': 1994,
                         'page_count': 607,
                         'genres': ['Novel', 'Science Fiction',
                                    'Pyschological Fiction']}
        return_json = {'name': 'Haruki Murakami',
                       'language': 'Japanese',
                       'title': 'Kafka on the shore',
                       'year_of_release': 2002,
                       'page_count': 505,
                       'genres': ['Novel', 'Magical Realism',
                                  'Fantasy Fiction']}

        self.assertDictEqual(return_json, expected_json)

    def test_dataclass(self):
        windup = Book(name='Windup Bird Chronicle', year=1994,
                      author='Haruki Murakami')
        kafka = Book(name='Kafka on the shore', year=2002,
                     author='Haruki Murakami')

        self.assertEqual(windup, kafka)


if __name__ == "__main__":
    unittest.main()
2 để so sánh sự bằng nhau của hai từ điển, phương pháp
import unittest

from dataclasses import dataclass

@dataclass
class Book:
    name: str
    year: int
    author: str


class TestUnitTestShowCase(unittest.TestCase):
    def test_assert_equal(self):
        expected_json = {'name': 'Haruki Murakami',
                         'language': 'Japanese',
                         'title': 'Windup Bird Chronicle',
                         'year_of_release': 1994,
                         'page_count': 607,
                         'genres': ['Novel', 'Science Fiction',
                                    'Pyschological Fiction']}
        return_json = {'name': 'Haruki Murakami',
                       'language': 'Japanese',
                       'title': 'Kafka on the shore',
                       'year_of_release': 2002,
                       'page_count': 505,
                       'genres': ['Novel', 'Magical Realism',
                                  'Fantasy Fiction']}

        self.assertDictEqual(return_json, expected_json)

    def test_dataclass(self):
        windup = Book(name='Windup Bird Chronicle', year=1994,
                      author='Haruki Murakami')
        kafka = Book(name='Kafka on the shore', year=2002,
                     author='Haruki Murakami')

        self.assertEqual(windup, kafka)


if __name__ == "__main__":
    unittest.main()
3 để so sánh sự bằng nhau của hai danh sách và phương pháp
import unittest

from dataclasses import dataclass

@dataclass
class Book:
    name: str
    year: int
    author: str


class TestUnitTestShowCase(unittest.TestCase):
    def test_assert_equal(self):
        expected_json = {'name': 'Haruki Murakami',
                         'language': 'Japanese',
                         'title': 'Windup Bird Chronicle',
                         'year_of_release': 1994,
                         'page_count': 607,
                         'genres': ['Novel', 'Science Fiction',
                                    'Pyschological Fiction']}
        return_json = {'name': 'Haruki Murakami',
                       'language': 'Japanese',
                       'title': 'Kafka on the shore',
                       'year_of_release': 2002,
                       'page_count': 505,
                       'genres': ['Novel', 'Magical Realism',
                                  'Fantasy Fiction']}

        self.assertDictEqual(return_json, expected_json)

    def test_dataclass(self):
        windup = Book(name='Windup Bird Chronicle', year=1994,
                      author='Haruki Murakami')
        kafka = Book(name='Kafka on the shore', year=2002,
                     author='Haruki Murakami')

        self.assertEqual(windup, kafka)


if __name__ == "__main__":
    unittest.main()
4 là tập hợp siêu cho tất cả các phép so sánh. Nó có thể hoạt động trên hai biến thực hiện kiểm tra đẳng thức. Vì vậy, về mặt kỹ thuật, có thể sử dụng
import unittest

from dataclasses import dataclass

@dataclass
class Book:
    name: str
    year: int
    author: str


class TestUnitTestShowCase(unittest.TestCase):
    def test_assert_equal(self):
        expected_json = {'name': 'Haruki Murakami',
                         'language': 'Japanese',
                         'title': 'Windup Bird Chronicle',
                         'year_of_release': 1994,
                         'page_count': 607,
                         'genres': ['Novel', 'Science Fiction',
                                    'Pyschological Fiction']}
        return_json = {'name': 'Haruki Murakami',
                       'language': 'Japanese',
                       'title': 'Kafka on the shore',
                       'year_of_release': 2002,
                       'page_count': 505,
                       'genres': ['Novel', 'Magical Realism',
                                  'Fantasy Fiction']}

        self.assertDictEqual(return_json, expected_json)

    def test_dataclass(self):
        windup = Book(name='Windup Bird Chronicle', year=1994,
                      author='Haruki Murakami')
        kafka = Book(name='Kafka on the shore', year=2002,
                     author='Haruki Murakami')

        self.assertEqual(windup, kafka)


if __name__ == "__main__":
    unittest.main()
4 thay vì
import unittest

from dataclasses import dataclass

@dataclass
class Book:
    name: str
    year: int
    author: str


class TestUnitTestShowCase(unittest.TestCase):
    def test_assert_equal(self):
        expected_json = {'name': 'Haruki Murakami',
                         'language': 'Japanese',
                         'title': 'Windup Bird Chronicle',
                         'year_of_release': 1994,
                         'page_count': 607,
                         'genres': ['Novel', 'Science Fiction',
                                    'Pyschological Fiction']}
        return_json = {'name': 'Haruki Murakami',
                       'language': 'Japanese',
                       'title': 'Kafka on the shore',
                       'year_of_release': 2002,
                       'page_count': 505,
                       'genres': ['Novel', 'Magical Realism',
                                  'Fantasy Fiction']}

        self.assertDictEqual(return_json, expected_json)

    def test_dataclass(self):
        windup = Book(name='Windup Bird Chronicle', year=1994,
                      author='Haruki Murakami')
        kafka = Book(name='Kafka on the shore', year=2002,
                     author='Haruki Murakami')

        self.assertEqual(windup, kafka)


if __name__ == "__main__":
    unittest.main()
2 và
import unittest

from dataclasses import dataclass

@dataclass
class Book:
    name: str
    year: int
    author: str


class TestUnitTestShowCase(unittest.TestCase):
    def test_assert_equal(self):
        expected_json = {'name': 'Haruki Murakami',
                         'language': 'Japanese',
                         'title': 'Windup Bird Chronicle',
                         'year_of_release': 1994,
                         'page_count': 607,
                         'genres': ['Novel', 'Science Fiction',
                                    'Pyschological Fiction']}
        return_json = {'name': 'Haruki Murakami',
                       'language': 'Japanese',
                       'title': 'Kafka on the shore',
                       'year_of_release': 2002,
                       'page_count': 505,
                       'genres': ['Novel', 'Magical Realism',
                                  'Fantasy Fiction']}

        self.assertDictEqual(return_json, expected_json)

    def test_dataclass(self):
        windup = Book(name='Windup Bird Chronicle', year=1994,
                      author='Haruki Murakami')
        kafka = Book(name='Kafka on the shore', year=2002,
                     author='Haruki Murakami')

        self.assertEqual(windup, kafka)


if __name__ == "__main__":
    unittest.main()
3. Việc nhớ sử dụng
import unittest

from dataclasses import dataclass

@dataclass
class Book:
    name: str
    year: int
    author: str


class TestUnitTestShowCase(unittest.TestCase):
    def test_assert_equal(self):
        expected_json = {'name': 'Haruki Murakami',
                         'language': 'Japanese',
                         'title': 'Windup Bird Chronicle',
                         'year_of_release': 1994,
                         'page_count': 607,
                         'genres': ['Novel', 'Science Fiction',
                                    'Pyschological Fiction']}
        return_json = {'name': 'Haruki Murakami',
                       'language': 'Japanese',
                       'title': 'Kafka on the shore',
                       'year_of_release': 2002,
                       'page_count': 505,
                       'genres': ['Novel', 'Magical Realism',
                                  'Fantasy Fiction']}

        self.assertDictEqual(return_json, expected_json)

    def test_dataclass(self):
        windup = Book(name='Windup Bird Chronicle', year=1994,
                      author='Haruki Murakami')
        kafka = Book(name='Kafka on the shore', year=2002,
                     author='Haruki Murakami')

        self.assertEqual(windup, kafka)


if __name__ == "__main__":
    unittest.main()
8 nào trở nên khá khó khăn. Một lợi thế của việc sử dụng các phương thức xác nhận đặc biệt là chúng kiểm tra loại
import unittest

from dataclasses import dataclass

@dataclass
class Book:
    name: str
    year: int
    author: str


class TestUnitTestShowCase(unittest.TestCase):
    def test_assert_equal(self):
        expected_json = {'name': 'Haruki Murakami',
                         'language': 'Japanese',
                         'title': 'Windup Bird Chronicle',
                         'year_of_release': 1994,
                         'page_count': 607,
                         'genres': ['Novel', 'Science Fiction',
                                    'Pyschological Fiction']}
        return_json = {'name': 'Haruki Murakami',
                       'language': 'Japanese',
                       'title': 'Kafka on the shore',
                       'year_of_release': 2002,
                       'page_count': 505,
                       'genres': ['Novel', 'Magical Realism',
                                  'Fantasy Fiction']}

        self.assertDictEqual(return_json, expected_json)

    def test_dataclass(self):
        windup = Book(name='Windup Bird Chronicle', year=1994,
                      author='Haruki Murakami')
        kafka = Book(name='Kafka on the shore', year=2002,
                     author='Haruki Murakami')

        self.assertEqual(windup, kafka)


if __name__ == "__main__":
    unittest.main()
9’ trước khi so sánh giá trị. Ví dụ:
def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
50 sẽ thất bại vì đối số đầu tiên không phải là từ điển

Pytest khuyên bạn nên sử dụng câu lệnh khẳng định trên bất kỳ chức năng hoặc phương thức chuyên biệt nào. Dưới đây là một ví dụ về các testcase pytest

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres

Một câu lệnh khẳng định có thể kiểm tra tất cả các loại bình đẳng. Thật đơn giản và dễ sử dụng và ghi nhớ

Pytest cũng hỗ trợ thực thi các Test kế thừa

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
51 với các phương thức xác nhận kiểm tra đơn vị hoặc các câu lệnh xác nhận. Bạn có thể viết các bài kiểm tra Pytest dưới dạng hàm

2. Thông báo lỗi tốt hơn trong Pytest

Xem xét hai bài kiểm tra unittest. Một kiểm tra hai từ điển bằng nhau và hai thể hiện của lớp dữ liệu bằng nhau

import unittest

from dataclasses import dataclass

@dataclass
class Book:
    name: str
    year: int
    author: str


class TestUnitTestShowCase(unittest.TestCase):
    def test_assert_equal(self):
        expected_json = {'name': 'Haruki Murakami',
                         'language': 'Japanese',
                         'title': 'Windup Bird Chronicle',
                         'year_of_release': 1994,
                         'page_count': 607,
                         'genres': ['Novel', 'Science Fiction',
                                    'Pyschological Fiction']}
        return_json = {'name': 'Haruki Murakami',
                       'language': 'Japanese',
                       'title': 'Kafka on the shore',
                       'year_of_release': 2002,
                       'page_count': 505,
                       'genres': ['Novel', 'Magical Realism',
                                  'Fantasy Fiction']}

        self.assertDictEqual(return_json, expected_json)

    def test_dataclass(self):
        windup = Book(name='Windup Bird Chronicle', year=1994,
                      author='Haruki Murakami')
        kafka = Book(name='Kafka on the shore', year=2002,
                     author='Haruki Murakami')

        self.assertEqual(windup, kafka)


if __name__ == "__main__":
    unittest.main()

đầu ra

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
5

Các giá trị khác nhau trên đối số đầu tiên bắt đầu bằng - dấu trừ và đối số thứ hai bắt đầu bằng dấu +

Khi hai giá trị từ điển khác nhau, thông báo lỗi sẽ in từng khóa và giá trị. Mặc dù các giá trị sai lệch được in ra, nhưng không dễ để nắm bắt các lỗi đầu ra của trình chạy thử nghiệm đơn vị

Khi sự khác biệt kéo dài hơn, bài kiểm tra đơn vị sẽ xử lý lại thông báo lỗi. Hành vi gây khó chịu khi thử nghiệm thất bại trong CI, mất nhiều thời gian để chạy và chạy lại sau khi thay đổi cấu hình

Đây là

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
52 đã sửa đổi

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
1

đầu ra

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
2

Đầu ra sau khi cài đặt

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
53

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
4

Bây giờ có một bản dịch phần tử mới bên trong từ điển. Do đó thông báo lỗi dài hơn. Các chi tiết của

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
54 bị thiếu trong một trong các từ điển và rất khó để tìm ra từ thông báo lỗi

Thông báo lỗi test_dataclass thử nghiệm thứ hai không cho biết thuộc tính nào khác

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
6

Thật khó để tìm ra những giá trị thuộc tính nào khác nhau khi lớp dữ liệu chứa 15 thuộc tính

Mặt khác, các thông báo lỗi pytest rõ ràng và nêu rõ sự khác biệt với mức độ dài dòng khác nhau

Đây là trường hợp thử nghiệm tương tự để chạy với Pytest

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
7

đầu ra

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
8

Thông báo lỗi không chỉ chứa thông báo lỗi mà còn toàn bộ định nghĩa chức năng để hiểu lỗi cùng với đủ thông tin không khớp

Hãy xem lỗi

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
55

  • def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    56 - Cho biết trong số tất cả các thuộc tính, một thuộc tính chứa cùng một giá trị

  • def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    57 - Nói tên của các thuộc tính khác nhau

  • def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    58 - Tiếp theo, thông báo lỗi đi sâu vào các giá trị thuộc tính khác nhau

  • def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    59 - Nói tên thuộc tính, in hai giá trị so sánh

  • def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    10 - Bây giờ Pytest xử lý lại thông báo lỗi còn lại nhưng cho biết có bao nhiêu dòng nữa, điều này khá hữu ích và nhà phát triển có thể quyết định chạy lại với cờ
    def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    11. Sau khi vượt qua cờ
    def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    11, đầu ra phong phú hơn

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
0

Đối với từ điển không khớp, Pytest in từng khóa và giá trị theo cùng một cách. Bằng cách có các cấp độ khác nhau này, các nhà phát triển sẽ sửa lỗi nhanh hơn

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
1

3. Tùy chọn dòng lệnh hữu ích

Pytest đi kèm với rất nhiều tùy chọn dòng lệnh hữu ích cho

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
13

Pytest có một tùy chọn

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
8 chỉ chạy thử nghiệm không thành công trong lần thực hiện cuối cùng

Đây là một ví dụ trong đó một bài kiểm tra thất bại

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
2

Tóm tắt ngắn gọn nói,

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
15. Lần tới, trong khi chạy thử nghiệm,
def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
16 chỉ thực hiện thử nghiệm không thành công từ lần chạy trước

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
3

Tóm tắt ngắn gọn nói,

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
17

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
18 thu thập tất cả các tệp kiểm tra và chức năng/lớp kiểm tra trong đường dẫn. Trong khi báo cáo,
def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
19 thông báo các tệp có cùng tên trên các thư mục khác nhau. Ví dụ: nếu có tệp
def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
20 trong thư mục kiểm tra đơn vị và trong thư mục tích hợp, pytest từ chối chạy thử nghiệm và cũng phàn nàn trong giai đoạn thu thập. Tên tệp thử nghiệm phải là duy nhất trong dự án

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
4

4. plugin pytest

Pytest được xây dựng bằng khung pluggy và nó được xây dựng dưới dạng một tập hợp các plugin có thể kết hợp và mở rộng. Do đó, bất kỳ nhà phát triển nào cũng có thể mở rộng chức năng của trình chạy pytest và chức năng kiểm tra pytest. Có hơn 300 plugin có sẵn

Trước đó trong bài đăng trên blog, tôi đã nói, văn bản thông báo lỗi pytest tốt hơn như thế nào so với

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
21. Kết xuất lỗi mặc định không hỗ trợ so sánh khác biệt được tô màu và so sánh cạnh nhau

Hai plugin

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
22 và
def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
23 cải thiện khả năng hiển thị thông báo lỗi và nội dung thông báo để hiểu rõ hơn và nhanh hơn

Một đầu ra pytest-icdiff mẫu

Python unittest khẳng định dict bằng

Đầu ra

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
23 mẫu với các thông báo lỗi nâng cao

Python unittest khẳng định dict bằng

Báo cáo

  • def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    25, https. //github. com/pytest-dev/pytest-cov là tích hợp pytest cho plugin bảo hiểm. Nó hỗ trợ tạo phạm vi bảo hiểm dưới dạng báo cáo HTML

  • def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    26, https. //github. com/pytest-dev/pytest-html/ là một plugin để tạo kết quả kiểm tra HTML. Khi không thành công, plugin sẽ chụp ảnh màn hình của trình duyệt thử nghiệm Selenium

  • def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    27, https. //pypi. org/project/pytest-xdist/ cho phép toàn bộ thử nghiệm python chạy trong nhiều CPU thay vì chạy trên một quy trình. Các đồ đạc có thể cần một chút thay đổi tùy thuộc vào việc thực hiện

  • Quy trình khám phá thử nghiệm mặc định hoạt động trong hầu hết các trường hợp, nhưng đối với các dự án lớn hơn cần nhiều thời gian để chạy, việc chạy thử nghiệm mới trước sẽ tiết kiệm rất nhiều thời gian. Khi kiểm thử không thành công, có thể dừng toàn bộ kiểm thử và chạy lại sau khi sửa lỗi. Khi chạy trong máy chủ CI, chức năng này có ích vì ba lý do - phản hồi nhanh hơn, tiết kiệm một vài đô la và giảm CI chờ PR đang chờ xử lý.

    def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    28, https. // kiểm tra. org/xác định-các bài kiểm tra bị ảnh hưởng. html sử dụng phạm vi bảo hiểm để tìm ra mã đã sửa đổi và lên lịch chạy mã đã sửa đổi trước

5. đồ đạc

Lịch thi đấu, , là dữ liệu hạt giống cho các bài kiểm tra. Ví dụ: để kiểm tra điểm cuối API, kiểm tra có thể cần

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
29. Với đồ đạc, cài đặt và xóa dữ liệu sau khi kiểm tra dễ dàng hơn. Pytest hỗ trợ năm phạm vi khác nhau cho những đồ đạc này -
def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
40. Đây là một ví dụ từ các tài liệu cho một vật cố định kết nối SMTP với phạm vi phiên

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
5

Ngoài việc tạo dữ liệu gốc cho cơ sở dữ liệu, có thể tạo mô hình hoặc nhà máy để thử nghiệm với sự trợ giúp của đồ đạc

  • def test_dictionary():
        rust = {'name': 'Rust', 'released': 2010}
        python = {'name': 'Python', 'released': 1989}
    
        assert rust == python
    
    def test_list():
        expected_genres = ['Novel', 'Literary Fiction']
        returned_genres = ['Novel', 'Popular Fiction']
    
        assert expected_genres == returned_genres
    
    41 cung cấp một tập hợp các điểm đánh dấu asyncio và đồ đạc để chạy thử nghiệm asyncio, https. //pypi. tổ chức/dự án/pytest-asyncio/

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
42 cung cấp một tập hợp các trình trợ giúp cụ thể của Django và đồ đạc để viết các trường hợp thử nghiệm ứng dụng Django. Điểm đánh dấu
def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
43 chỉ cho phép các trường hợp thử nghiệm được đánh dấu để truy cập cơ sở dữ liệu. Đây là một tính năng tiện dụng để tách quyền truy cập cơ sở dữ liệu. Trình trợ giúp
def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
44 chỉ cho phép
def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
45 lần truy cập cơ sở dữ liệu trong một chức năng hoặc phương thức kiểm tra. Có khá nhiều trình trợ giúp hữu ích trong gói, pytest-django

Nhìn chung, Pytest là một thư viện mạnh mẽ, giàu tính năng để viết các trường hợp thử nghiệm tốt hơn. Thư viện sử dụng nhiều chức năng và trình trang trí hơn để triển khai, mở rộng các tính năng cốt lõi. Đặc biệt là viết và hiểu đồ đạc liên quan đến một chút đường cong học tập, đồng thời, đồ đạc pytest có thể mở rộng và mạnh mẽ. Tính năng mạnh mẽ khác là tham số hóa chức năng có thể tiết kiệm rất nhiều mã nồi hơi

Nói chung, pytest là khung thử nghiệm mạnh mẽ hơn, có thể mở rộng và có thể định cấu hình so với khung

def test_dictionary():
    rust = {'name': 'Rust', 'released': 2010}
    python = {'name': 'Python', 'released': 1989}

    assert rust == python

def test_list():
    expected_genres = ['Novel', 'Literary Fiction']
    returned_genres = ['Novel', 'Popular Fiction']

    assert expected_genres == returned_genres
21 trong thư viện chuẩn. Bạn vẫn có thể kế thừa
import unittest

from dataclasses import dataclass

@dataclass
class Book:
    name: str
    year: int
    author: str


class TestUnitTestShowCase(unittest.TestCase):
    def test_assert_equal(self):
        expected_json = {'name': 'Haruki Murakami',
                         'language': 'Japanese',
                         'title': 'Windup Bird Chronicle',
                         'year_of_release': 1994,
                         'page_count': 607,
                         'genres': ['Novel', 'Science Fiction',
                                    'Pyschological Fiction']}
        return_json = {'name': 'Haruki Murakami',
                       'language': 'Japanese',
                       'title': 'Kafka on the shore',
                       'year_of_release': 2002,
                       'page_count': 505,
                       'genres': ['Novel', 'Magical Realism',
                                  'Fantasy Fiction']}

        self.assertDictEqual(return_json, expected_json)

    def test_dataclass(self):
        windup = Book(name='Windup Bird Chronicle', year=1994,
                      author='Haruki Murakami')
        kafka = Book(name='Kafka on the shore', year=2002,
                     author='Haruki Murakami')

        self.assertEqual(windup, kafka)


if __name__ == "__main__":
    unittest.main()
1 và sử dụng pytest làm trình chạy thử. Rất đáng để đầu tư thời gian tìm hiểu và sử dụng nó

Làm cách nào để sử dụng Assertdictequal trong Python?

assertEqual() trong Python là một hàm thư viện đơn vị nhất được sử dụng trong thử nghiệm đơn vị để kiểm tra sự bằng nhau của hai giá trị . Hàm này sẽ lấy ba tham số làm đầu vào và trả về giá trị boolean tùy thuộc vào điều kiện xác nhận. Nếu cả hai giá trị đầu vào đều bằng assertEqual() sẽ trả về true, ngược lại trả về false.

Sự khác biệt giữa Pytest và unittest là gì?

Unittest yêu cầu các nhà phát triển tạo các lớp bắt nguồn từ mô-đun TestCase và sau đó xác định các trường hợp thử nghiệm dưới dạng các phương thức trong lớp. Mặt khác, Pytest chỉ yêu cầu bạn xác định một hàm với “test_” được thêm vào trước và sử dụng các điều kiện xác nhận bên trong chúng

Các phương pháp hay nhất để thử nghiệm đơn vị Python là gì?

Kiểm tra đơn vị Python. Các phương pháp hay nhất nên tuân theo .
Tránh toàn cầu cấp mô-đun. .
Toàn cầu không phải lúc nào cũng xấu. .
Sử dụng tiêm phụ thuộc

Làm cách nào để chạy Python unittest từ dòng lệnh?

Vậy lệnh chạy python unittest sẽ là. $python3. 6 -m unittest Basic_Test . Kiểm tra Nếu bạn muốn xem dài dòng, thì lệnh sẽ là; .