Hướng dẫn assert dict equal python - khẳng định dict bằng python

Về cơ bản, nó cho phép

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
9 cung cấp cho bạn thêm thông tin về lý do tại sao thử nghiệm thất bại ("Chẩn đoán", sử dụng ngôn ngữ từ "phần mềm hướng đối tượng đang phát triển được hướng dẫn bởi các thử nghiệm" của Steve Freeman và Nat Pryce). So sánh hai bài kiểm tra sau:

import unittest


class DemoTest(unittest.TestCase):

    D1 = {'a': 1, 'b': 2, 'c': [1, 2]}
    D2 = {'a': 1, 'b': 2, 'c': [1]}

    def test_not_so_useful(self):
        self.assertTrue(self.D1 == self.D2)

    def test_useful(self):
        self.assertDictEqual(self.D1, self.D2)


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

Và đầu ra của chúng:

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true

vs.

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 

Sau này, bạn có thể thấy chính xác sự khác biệt là gì, bạn không phải tự mình giải quyết. Lưu ý rằng bạn chỉ có thể sử dụng

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
0 tiêu chuẩn thay vì
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
1, với cùng một kết quả; mỗi tài liệu

... Nó thường không cần thiết để gọi các phương pháp này trực tiếp.

________ 185 ________ 805 (kết quả) ¶ Lib/unittest/__init__.py


Loại bỏ một kết quả đã đăng ký. Khi kết quả đã được xóa thì

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
20 sẽ không còn được gọi trên đối tượng kết quả đó để đáp ứng với Control-C.the list of assert methods.)

________ 185 ________ 808 (hàm = không) ¶

Khi được gọi mà không có đối số, chức năng này sẽ loại bỏ trình xử lý điều khiển-C nếu nó đã được cài đặt. Chức năng này cũng có thể được sử dụng như một công cụ trang trí thử nghiệm để tạm thời loại bỏ trình xử lý trong khi thử nghiệm đang được thực thi:

Mã nguồn: lib/unittest/__ init__.py

(Nếu bạn đã quen thuộc với các khái niệm cơ bản của thử nghiệm, bạn có thể muốn bỏ qua danh sách các phương thức khẳng định.)

Khung thử nghiệm đơn vị
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2 ban đầu được lấy cảm hứng từ Junit và có hương vị tương tự như các khung thử nghiệm đơn vị chính trong các ngôn ngữ khác. Nó hỗ trợ tự động hóa thử nghiệm, chia sẻ mã thiết lập và tắt máy để kiểm tra, tổng hợp các bài kiểm tra thành các bộ sưu tập và tính độc lập của các bài kiểm tra từ khung báo cáo.

Để đạt được điều này,

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2 hỗ trợ một số khái niệm quan trọng theo cách hướng đối tượng:

thử nghiệm cố định

Một vật cố thử nghiệm thể hiện sự chuẩn bị cần thiết để thực hiện một hoặc nhiều bài kiểm tra và bất kỳ hành động dọn dẹp liên quan nào. Điều này có thể liên quan đến, ví dụ, tạo cơ sở dữ liệu, thư mục tạm thời hoặc proxy hoặc bắt đầu một quy trình máy chủ.

Trường hợp kiểm tra

Một trường hợp thử nghiệm là đơn vị thử nghiệm riêng lẻ. Nó kiểm tra một phản hồi cụ thể cho một tập hợp đầu vào cụ thể.

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2 cung cấp một lớp cơ sở,
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5, có thể được sử dụng để tạo các trường hợp thử nghiệm mới.

Xem thêm

Mô -đun
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
6

Một mô-đun hỗ trợ thử nghiệm khác với một hương vị rất khác nhau.

Thử nghiệm nhỏ đơn giản: Với các mẫu

Bài viết gốc của Kent Beck, về các khung thử nghiệm sử dụng mẫu được chia sẻ bởi

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2.

pytest

Khung Unittest của bên thứ ba với cú pháp trọng lượng nhẹ hơn để viết bài kiểm tra. Ví dụ,

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
8.

Các công cụ thử nghiệm Python phân loại

Một danh sách rộng rãi các công cụ thử nghiệm Python bao gồm các khung thử nghiệm chức năng và thư viện đối tượng giả.

Kiểm tra trong danh sách gửi thư Python

Một nhóm lợi ích đặc biệt để thảo luận về các công cụ thử nghiệm và thử nghiệm, trong Python.

Tập lệnh

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
9 trong phân phối nguồn Python là một công cụ GUI để khám phá và thực thi thử nghiệm. Điều này được dự định phần lớn để dễ sử dụng cho những người mới để kiểm tra đơn vị. Đối với môi trường sản xuất, các thử nghiệm nên được điều khiển bởi một hệ thống tích hợp liên tục như Buildbot, Jenkins hoặc Travis-CI hoặc AppVeyor.

Ví dụ cơ bản

Mô -đun

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2 cung cấp một bộ công cụ phong phú để xây dựng và chạy thử nghiệm. Phần này chứng minh rằng một tập hợp con nhỏ của các công cụ đủ để đáp ứng nhu cầu của hầu hết người dùng.

Dưới đây là một tập lệnh ngắn để kiểm tra ba phương thức chuỗi:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

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

Một testcase được tạo ra bằng cách phân lớp

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
1. Ba thử nghiệm riêng lẻ được xác định bằng các phương thức có tên bắt đầu bằng các chữ cái
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
2. Công ước đặt tên này thông báo cho người chạy thử nghiệm về phương pháp nào đại diện cho các thử nghiệm.

Mấu chốt của mỗi bài kiểm tra là một cuộc gọi đến

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
3 để kiểm tra kết quả dự kiến;
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
4 hoặc
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
5 để xác minh một điều kiện; hoặc
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 để xác minh rằng một ngoại lệ cụ thể được nâng lên. Các phương pháp này được sử dụng thay vì câu lệnh
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
7 để người chạy thử có thể tích lũy tất cả các kết quả kiểm tra và tạo báo cáo.

Các phương thức

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
8 và
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 cho phép bạn xác định các hướng dẫn sẽ được thực thi trước và sau mỗi phương thức kiểm tra. Chúng được đề cập chi tiết hơn trong phần tổ chức mã kiểm tra.Organizing test code.

Khối cuối cùng cho thấy một cách đơn giản để chạy các bài kiểm tra.

python -m unittest tests/test_something.py
0 cung cấp giao diện dòng lệnh cho tập lệnh kiểm tra. Khi chạy từ dòng lệnh, tập lệnh trên tạo ra một đầu ra trông như thế này:

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

Chuyển tùy chọn

python -m unittest tests/test_something.py
1 cho tập lệnh kiểm tra của bạn sẽ hướng dẫn
python -m unittest tests/test_something.py
0 để cho phép mức độ xác thực cao hơn và tạo ra đầu ra sau:

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK

Các ví dụ trên cho thấy các tính năng

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2 được sử dụng phổ biến nhất đủ để đáp ứng nhiều nhu cầu thử nghiệm hàng ngày. Phần còn lại của tài liệu khám phá toàn bộ tính năng được đặt từ các nguyên tắc đầu tiên.

Giao diện dòng lệnh

Mô -đun Unittest có thể được sử dụng từ dòng lệnh để chạy các thử nghiệm từ các mô -đun, lớp hoặc thậm chí các phương thức kiểm tra riêng lẻ:

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method

Bạn có thể chuyển trong danh sách với bất kỳ kết hợp tên mô -đun và tên lớp hoặc phương thức đủ điều kiện.

Các mô -đun kiểm tra cũng có thể được chỉ định theo đường dẫn tệp:

python -m unittest tests/test_something.py

Điều này cho phép bạn sử dụng hoàn thành tên tệp shell để chỉ định mô -đun thử nghiệm. Tệp được chỉ định vẫn có thể nhập dưới dạng mô -đun. Đường dẫn được chuyển đổi thành một tên mô -đun bằng cách loại bỏ ‘.py, và chuyển đổi phân tách đường dẫn thành‘. Nếu bạn muốn thực thi một tệp kiểm tra không thể nhập như một mô -đun, bạn nên thực thi trực tiếp tệp.

Bạn có thể chạy các bài kiểm tra với nhiều chi tiết hơn (độ merbosity cao hơn) bằng cách chuyển trong cờ -V:

python -m unittest -v test_module

Khi được thực hiện mà không bắt đầu khám phá kiểm tra đối số được bắt đầu:Test Discovery is started:

Đối với danh sách tất cả các tùy chọn dòng lệnh:

Thay đổi trong phiên bản 3.2: Trong các phiên bản trước, chỉ có thể chạy các phương thức thử nghiệm riêng lẻ và không phải các mô -đun hoặc lớp.In earlier versions it was only possible to run individual test methods and not modules or classes.

Tùy chọn dòng lệnh mà

Unittest hỗ trợ các tùy chọn dòng lệnh này: supports these command-line options:

________ 74 ________ 75 ________ 76 ________ 77 ________ 75¶

Đầu ra tiêu chuẩn và các luồng lỗi tiêu chuẩn được đệm trong quá trình chạy thử. Đầu ra trong một bài kiểm tra đi qua bị loại bỏ. Đầu ra được lặp lại bình thường khi kiểm tra thất bại hoặc lỗi và được thêm vào các tin nhắn thất bại.

________ 79 ________ 75 ________ 76 ________ 82 ________ 75¶

Control-C trong quá trình chạy thử nghiệm chờ thử nghiệm hiện tại kết thúc và sau đó báo cáo tất cả các kết quả cho đến nay. Một điều khiển thứ hai-C làm tăng ngoại lệ

python -m unittest -v test_module
4 bình thường.

Xem Xử lý tín hiệu cho các chức năng cung cấp chức năng này.

________ 85 ________ 75 ________ 76 ________ 88 ________ 75¶

Dừng kiểm tra chạy trên lỗi hoặc thất bại đầu tiên.

________ 90 ________ 75¶

Chỉ chạy các phương thức kiểm tra và các lớp phù hợp với mẫu hoặc chuỗi con. Tùy chọn này có thể được sử dụng nhiều lần, trong trường hợp đó tất cả các trường hợp thử nghiệm khớp với bất kỳ mẫu nào được đưa vào.

Các mẫu chứa ký tự ký tự đại diện (

cd project_directory
python -m unittest discover
2) được khớp với tên thử nghiệm bằng cách sử dụng
cd project_directory
python -m unittest discover
3; Nếu không, kết hợp phụ nhạy cảm trường hợp đơn giản được sử dụng.

Các mẫu được khớp với tên phương thức thử nghiệm đủ điều kiện được nhập bởi trình tải thử nghiệm.

Ví dụ:

cd project_directory
python -m unittest discover
4 khớp với
cd project_directory
python -m unittest discover
5,
cd project_directory
python -m unittest discover
6, nhưng không phải
cd project_directory
python -m unittest discover
7.

________ 98 ________ 75¶

Hiển thị các biến cục bộ trong Tracebacks.

Mới trong phiên bản 3.2: Các tùy chọn dòng lệnh

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
00,
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
01 và
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
02 đã được thêm vào.The command-line options
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
00,
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
01 and
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
02 were added.

Mới trong phiên bản 3.5: Tùy chọn dòng lệnh

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
03.The command-line option
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
03.

Mới trong phiên bản 3.7: Tùy chọn dòng lệnh

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
04.The command-line option
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
04.

Dòng lệnh cũng có thể được sử dụng để khám phá thử nghiệm, để chạy tất cả các thử nghiệm trong một dự án hoặc chỉ là một tập hợp con.

Thử nghiệm khám phá

Mới trong phiên bản 3.2.

Unittest hỗ trợ khám phá thử nghiệm đơn giản. Để tương thích với khám phá thử nghiệm, tất cả các tệp thử nghiệm phải là các mô-đun hoặc gói (bao gồm các gói tên không gian tên) có thể nhập từ thư mục cấp cao nhất của dự án (điều này có nghĩa là tên tệp của chúng phải là định danh hợp lệ).modules or packages (including namespace packages) importable from the top-level directory of the project (this means that their filenames must be valid identifiers).

Khám phá thử nghiệm được triển khai trong

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
05, nhưng cũng có thể được sử dụng từ dòng lệnh. Việc sử dụng dòng lệnh cơ bản là:

cd project_directory
python -m unittest discover

Ghi chú

Như một lối tắt,

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
06 là tương đương với
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
07. Nếu bạn muốn vượt qua các đối số để kiểm tra khám phá, lệnh phụ ____108 phải được sử dụng rõ ràng.

Trình lệnh phụ

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
08 có các tùy chọn sau:

________ 110 ________ 75 ________ 76 ________ 113 ________ 75¶

Báo cáo dài dòng

________ 115 ________ 75 ________ 76 ________ 118 ________ 119¶

Thư mục để bắt đầu khám phá (mặc định

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
20)

________ 121 ________ 75 ________ 76 ________ 124 ________ 125¶

Mẫu để khớp các tệp kiểm tra (mặc định

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
26)

________ 127 ________ 75 ________ 76 ________ 130 ________ 119¶

Thư mục cấp cao nhất của dự án (mặc định để bắt đầu thư mục)

Các tùy chọn

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
32,
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
33 và
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
34 có thể được truyền trong các đối số vị trí theo thứ tự đó. Hai dòng lệnh sau đây tương đương:

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
0

Cũng như là một đường dẫn, có thể chuyển tên gói, ví dụ

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
35, làm thư mục bắt đầu. Tên gói bạn cung cấp sau đó sẽ được nhập và vị trí của nó trên hệ thống tập tin sẽ được sử dụng làm thư mục bắt đầu.

Thận trọng

Kiểm tra khám phá tải thử nghiệm bằng cách nhập chúng. Khi Test Discovery đã tìm thấy tất cả các tệp thử nghiệm từ thư mục bắt đầu, bạn chỉ định rằng nó biến các đường dẫn thành tên gói để nhập. Ví dụ

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
36 sẽ được nhập dưới dạng
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
37.

Nếu bạn có một gói được cài đặt trên toàn cầu và thử khám phá thử nghiệm trên một bản sao khác của gói thì việc nhập có thể xảy ra từ nơi sai. Nếu điều này xảy ra khám phá kiểm tra sẽ cảnh báo bạn và thoát.

Nếu bạn cung cấp thư mục bắt đầu dưới dạng tên gói chứ không phải là đường dẫn đến thư mục thì hãy khám phá giả định rằng bất kỳ vị trí nào nó nhập từ vị trí bạn dự định, vì vậy bạn sẽ không nhận được cảnh báo.

Các mô -đun và gói kiểm tra có thể tùy chỉnh tải và khám phá thử nghiệm bằng cách thông qua giao thức load_tests.

Đã thay đổi trong phiên bản 3.4: Thử nghiệm khám phá hỗ trợ các gói không gian tên cho thư mục bắt đầu. Lưu ý rằng bạn cũng cần chỉ định thư mục cấp cao nhất (ví dụ:

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
38).Test discovery supports namespace packages for the start directory. Note that you need to specify the top level directory too (e.g.
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
38).

Tổ chức mã kiểm tra

Các khối xây dựng cơ bản của thử nghiệm đơn vị là các trường hợp thử nghiệm - các kịch bản đơn phải được thiết lập và kiểm tra tính đúng đắn. Trong

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2, các trường hợp thử nghiệm được biểu thị bằng các trường hợp
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
1. Để thực hiện các trường hợp kiểm tra của riêng bạn, bạn phải viết các lớp con là
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 hoặc sử dụng
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
42.

Mã thử nghiệm của một ví dụ

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 phải hoàn toàn khép kín, sao cho nó có thể được chạy trong sự cô lập hoặc kết hợp tùy ý với bất kỳ số lượng trường hợp thử nghiệm nào khác.

Lớp con

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 đơn giản nhất sẽ chỉ cần thực hiện một phương thức kiểm tra (nghĩa là một phương thức có tên bắt đầu bằng
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
2) để thực hiện mã kiểm tra cụ thể:

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
1

Lưu ý rằng để kiểm tra một cái gì đó, chúng tôi sử dụng một trong các phương thức

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
46 được cung cấp bởi lớp cơ sở
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5. Nếu thử nghiệm thất bại, một ngoại lệ sẽ được nêu ra với một thông điệp giải thích và
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2 sẽ xác định trường hợp thử nghiệm là một lỗi. Bất kỳ trường hợp ngoại lệ khác sẽ được coi là lỗi.

Các bài kiểm tra có thể rất nhiều, và thiết lập của chúng có thể lặp đi lặp lại. May mắn thay, chúng tôi có thể đưa ra mã thiết lập bằng cách triển khai một phương thức gọi là

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
8, mà khung thử nghiệm sẽ tự động gọi cho mỗi thử nghiệm chúng tôi chạy:

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
2

Ghi chú

Như một lối tắt,

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
06 là tương đương với
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
07. Nếu bạn muốn vượt qua các đối số để kiểm tra khám phá, lệnh phụ ____108 phải được sử dụng rõ ràng.

Trình lệnh phụ

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
08 có các tùy chọn sau:

________ 110 ________ 75 ________ 76 ________ 113 ________ 75¶

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
3

Nếu

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
8 thành công,
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 sẽ được chạy cho dù phương pháp kiểm tra có thành công hay không.

Một môi trường làm việc như vậy cho mã thử nghiệm được gọi là vật cố thử nghiệm. Một phiên bản TestCase mới được tạo như một vật cố thử nghiệm duy nhất được sử dụng để thực hiện từng phương pháp thử nghiệm riêng lẻ. Do đó,

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
8,
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 và
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
56 sẽ được gọi một lần mỗi lần thử nghiệm.

Bạn nên sử dụng các triển khai TestCase để các thử nghiệm nhóm cùng nhau theo các tính năng mà họ kiểm tra.

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2 cung cấp một cơ chế cho điều này: Bộ thử nghiệm, được biểu thị bằng lớp ____ 52 ____ ____159. Trong hầu hết các trường hợp, gọi
python -m unittest tests/test_something.py
0 sẽ làm điều đúng đắn và thu thập tất cả các trường hợp thử nghiệm mô -đun cho bạn và thực hiện chúng.

Tuy nhiên, nếu bạn muốn tùy chỉnh việc xây dựng bộ thử nghiệm của mình, bạn có thể tự làm điều đó:

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
4

Bạn có thể đặt các định nghĩa về các trường hợp thử nghiệm và bộ thử nghiệm trong cùng một mô -đun với mã chúng sẽ kiểm tra (chẳng hạn như

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
61), nhưng có một số lợi thế để đặt mã kiểm tra trong một mô -đun riêng, chẳng hạn như
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
62:

  • Mô -đun thử nghiệm có thể được chạy độc lập từ dòng lệnh.

  • Mã kiểm tra có thể dễ dàng được tách ra khỏi mã được vận chuyển.

  • Có ít cám dỗ hơn để thay đổi mã kiểm tra để phù hợp với mã mà nó kiểm tra mà không có lý do chính đáng.

  • Mã kiểm tra nên được sửa đổi ít thường xuyên hơn nhiều so với mã mà nó kiểm tra.

  • Mã được kiểm tra có thể được tái cấu trúc dễ dàng hơn.

  • Các thử nghiệm cho các mô -đun được viết trong C phải ở trong các mô -đun riêng biệt, vậy tại sao không nhất quán?

  • Nếu chiến lược thử nghiệm thay đổi, không cần phải thay đổi mã nguồn.

Tái sử dụng mã kiểm tra cũ

Một số người dùng sẽ thấy rằng họ có mã kiểm tra hiện tại mà họ muốn chạy từ

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2, mà không chuyển đổi mọi chức năng kiểm tra cũ thành lớp con
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5.

Vì lý do này,

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2 cung cấp một lớp
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
42. Lớp con này của
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 có thể được sử dụng để bọc một chức năng kiểm tra hiện có. Các chức năng thiết lập và xé nát cũng có thể được cung cấp.

Cho chức năng kiểm tra sau:

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
5

Người ta có thể tạo một trường hợp kiểm tra tương đương như sau, với các phương thức thiết lập và phá vỡ tùy chọn:

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
6

Ghi chú

Mặc dù

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
42 có thể được sử dụng để nhanh chóng chuyển đổi một cơ sở thử nghiệm hiện có sang hệ thống dựa trên ________ 52, phương pháp này không được khuyến khích. Dành thời gian để thiết lập các lớp con
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 thích hợp sẽ làm cho việc tái cấu trúc thử nghiệm trong tương lai dễ dàng hơn.

Trong một số trường hợp, các thử nghiệm hiện tại có thể đã được viết bằng mô -đun

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
6. Nếu vậy,
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
6 cung cấp một lớp
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
73 có thể tự động xây dựng các trường hợp
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
74 từ các thử nghiệm dựa trên ________ 56 hiện có.

Bỏ qua các bài kiểm tra và thất bại dự kiến ​​Jo

Mới trong phiên bản 3.1.

Unittest hỗ trợ bỏ qua các phương pháp kiểm tra riêng lẻ và thậm chí cả các lớp kiểm tra. Ngoài ra, nó hỗ trợ đánh dấu một bài kiểm tra là một thất bại mong đợi của người Viking, một thử nghiệm bị hỏng và sẽ thất bại, nhưng không nên tính là một thất bại trên

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76.

Bỏ qua một bài kiểm tra chỉ đơn giản là vấn đề sử dụng trình trang trí

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
77 hoặc một trong những biến thể có điều kiện của nó, gọi
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
78 trong phương pháp
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
8 hoặc thử nghiệm hoặc tăng trực tiếp
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
80.decorator or one of its conditional variants, calling
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
78 within a
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
8 or test method, or raising
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
80 directly.

Bỏ qua cơ bản trông như thế này:

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
7

Đây là đầu ra của việc chạy ví dụ trên trong chế độ Verbose:

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
8

Các lớp có thể được bỏ qua giống như các phương pháp:

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
9

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
81 cũng có thể bỏ qua bài kiểm tra. Điều này rất hữu ích khi một tài nguyên cần được thiết lập không có sẵn.

Thất bại dự kiến ​​sử dụng công cụ trang trí

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
82.

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
0

Nó rất dễ dàng để cuộn các nhà trang trí bỏ qua của riêng bạn bằng cách thực hiện một người trang trí gọi

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
77 trong bài kiểm tra khi nó muốn nó bị bỏ qua. Người trang trí này bỏ qua bài kiểm tra trừ khi đối tượng được truyền có một thuộc tính nhất định:

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
1

Các nhà trang trí sau đây và ngoại lệ thực hiện bỏ qua thử nghiệm và thất bại dự kiến:

________ 184 ________ 185 ________ 186 (lý do) ¶(reason)

Bỏ qua vô điều kiện các bài kiểm tra trang trí. Lý do nên mô tả lý do tại sao bài kiểm tra đang bị bỏ qua.

________ 184 ________ 185 ________ 189 (điều kiện, lý do) ¶(condition, reason)

Bỏ qua bài kiểm tra trang trí nếu điều kiện là đúng.

________ 184 ________ 185 ________ 192 (điều kiện, lý do) ¶(condition, reason)

Bỏ qua bài kiểm tra trang trí trừ khi điều kiện là đúng.

________ 184 ________ 185 ________ 195¶

Đánh dấu bài kiểm tra là một lỗi hoặc lỗi dự kiến. Nếu thử nghiệm không thành công hoặc lỗi trong chính chức năng thử nghiệm (thay vì trong một trong các phương thức cố định thử nghiệm) thì nó sẽ được coi là thành công. Nếu bài kiểm tra vượt qua, nó sẽ được coi là một thất bại.

Ngoại lệ ________ 185 ________ 197 (lý do) ¶(reason)

Ngoại lệ này được nâng lên để bỏ qua một bài kiểm tra.

Thông thường bạn có thể sử dụng

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
78 hoặc một trong những người trang trí bỏ qua thay vì nâng này trực tiếp.

Các bài kiểm tra bỏ qua sẽ không có

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
8 hoặc
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 chạy xung quanh chúng. Các lớp bị bỏ qua sẽ không có
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
01 hoặc
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
02 chạy. Các mô -đun bỏ qua sẽ không có
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
03 hoặc
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
04.

Phân biệt các lần lặp kiểm tra bằng cách sử dụng các bài kiểm tra

Mới trong phiên bản 3.4.

Ví dụ, khi có sự khác biệt rất nhỏ giữa các bài kiểm tra của bạn, ví dụ, một số tham số, Unittest cho phép bạn phân biệt chúng bên trong phần thân của phương pháp thử nghiệm bằng cách sử dụng trình quản lý bối cảnh

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
05.

Ví dụ: bài kiểm tra sau:

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
2

sẽ tạo ra đầu ra sau:

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
3

Nếu không sử dụng phép trừ, việc thực thi sẽ dừng sau lần thất bại đầu tiên và lỗi sẽ ít dễ chẩn đoán hơn vì giá trị của

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
06 sẽ được hiển thị:

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
4

Các lớp học và chức năng

Phần này mô tả độ sâu API của

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2.

Các trường hợp kiểm tra Jo

Lớp ________ 185 ________ 209 (Phương thứcName = 'RunTest') ¶(methodName='runTest')

Các trường hợp của lớp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 đại diện cho các đơn vị kiểm tra logic trong vũ trụ
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2. Lớp này được dự định sẽ được sử dụng làm lớp cơ sở, với các thử nghiệm cụ thể được thực hiện bởi các lớp con bê tông. Lớp này thực hiện giao diện cần thiết cho người chạy thử để cho phép nó điều khiển các thử nghiệm và các phương thức mà mã kiểm tra có thể sử dụng để kiểm tra và báo cáo các loại lỗi khác nhau.

Mỗi phiên bản của

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 sẽ chạy một phương thức cơ sở duy nhất: phương thức có tên Phương thức. Trong hầu hết các cách sử dụng của
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5, bạn sẽ không thay đổi tên phương thức cũng như tái tạo phương thức
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
14 mặc định.

Đã thay đổi trong phiên bản 3.2:

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 có thể được khởi tạo thành công mà không cần cung cấp tên phương thức. Điều này giúp dễ dàng thử nghiệm với
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 từ trình thông dịch tương tác.
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 can be instantiated successfully without providing a methodName. This makes it easier to experiment with
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 from the interactive interpreter.

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 Các trường hợp cung cấp ba nhóm phương pháp: một nhóm được sử dụng để chạy thử nghiệm, một nhóm khác được sử dụng bởi việc thực hiện thử nghiệm để kiểm tra các điều kiện và báo cáo thất bại và một số phương pháp điều tra cho phép thông tin về bài kiểm tra được thu thập.

Các phương thức trong nhóm đầu tiên (chạy thử nghiệm) là:

________ 218 ()()

Phương pháp được gọi để chuẩn bị vật cố thử nghiệm. Điều này được gọi ngay lập tức trước khi gọi phương thức kiểm tra; Khác với

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
19 hoặc
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
80, bất kỳ ngoại lệ nào được đưa ra bởi phương pháp này sẽ được coi là một lỗi thay vì lỗi thử nghiệm. Việc thực hiện mặc định không có gì.

________ 221 ()()

Phương thức được gọi ngay sau khi phương pháp thử nghiệm đã được gọi và kết quả được ghi lại. Điều này được gọi ngay cả khi phương pháp kiểm tra nêu ra một ngoại lệ, vì vậy việc triển khai trong các lớp con có thể cần đặc biệt cẩn thận về việc kiểm tra trạng thái nội bộ. Bất kỳ ngoại lệ nào, ngoài

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
19 hoặc
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
80, được đưa ra bởi phương pháp này sẽ được coi là một lỗi bổ sung thay vì lỗi kiểm tra (do đó làm tăng tổng số lỗi được báo cáo). Phương pháp này sẽ chỉ được gọi nếu
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
8 thành công, bất kể kết quả của phương pháp thử nghiệm. Việc thực hiện mặc định không có gì.

________ 225 ()()

Một phương thức lớp được gọi trước khi kiểm tra trong một lớp riêng lẻ được chạy.

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
26 được gọi với lớp là đối số duy nhất và phải được trang trí dưới dạng
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
27:

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
5

Xem đồ đạc lớp và mô -đun để biết thêm chi tiết.

Mới trong phiên bản 3.2.

________ 228 ()()

Một phương pháp lớp được gọi sau khi kiểm tra trong một lớp riêng lẻ đã chạy.

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
29 được gọi với lớp là đối số duy nhất và phải được trang trí dưới dạng
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
27:

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
6

Xem đồ đạc lớp và mô -đun để biết thêm chi tiết.

Mới trong phiên bản 3.2.

________ 228 ()(result=None)

Một phương pháp lớp được gọi sau khi kiểm tra trong một lớp riêng lẻ đã chạy.

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
29 được gọi với lớp là đối số duy nhất và phải được trang trí dưới dạng
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
27:

________ 231 (result = none) ¶

Chạy thử nghiệm, thu thập kết quả vào đối tượng

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76 được truyền qua kết quả. Nếu kết quả bị bỏ qua hoặc
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33, một đối tượng kết quả tạm thời được tạo (bằng cách gọi phương thức
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
34) và được sử dụng. Đối tượng kết quả được trả lại cho người gọi ____ 235.Previous versions of
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
37 did not return the result. Neither did calling an instance.

Hiệu ứng tương tự có thể có bằng cách gọi đơn giản là phiên bản
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5.(reason)

Đã thay đổi trong phiên bản 3.3: Các phiên bản trước của

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
37 không trả về kết quả. Không gọi một ví dụ.Skipping tests and expected failures for more information.

________ 238 (lý do) ¶

Gọi điều này trong một phương pháp kiểm tra hoặc
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
8 bỏ qua bài kiểm tra hiện tại. Xem các bài kiểm tra bỏ qua và thất bại dự kiến ​​để biết thêm thông tin.(msg=None, **params)

Mới trong phiên bản 3.1.

________ 240 (msg = none, ** params) ¶

Trả về Trình quản lý ngữ cảnh thực thi khối mã kèm theo dưới dạng phép trừ. MSG và params là các giá trị tùy chọn, tùy ý được hiển thị bất cứ khi nào một sự kiện trừ, cho phép bạn xác định chúng rõ ràng.Distinguishing test iterations using subtests for more information.

Mới trong phiên bản 3.4.

Một trường hợp thử nghiệm có thể chứa bất kỳ số lượng khai báo phụ nhất và chúng có thể được lồng tùy ý.()

Xem các lần lặp kiểm tra phân biệt bằng cách sử dụng các bài kiểm tra để biết thêm thông tin.

________ 241 ()

Phương pháp

Kiểm tra điều đó

Mới

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
43

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
44

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
45

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
46

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
47

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
48

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
49

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
50

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
51

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
52

3.1

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
53

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
54

3.1

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
55

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
56

3.1

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
57

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
58

3.1

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
59

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
60

3.1

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
61

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
62

3.1

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
63

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
64

3.2

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
65

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
66

3.2

Tất cả các phương thức khẳng định chấp nhận một đối số MSG rằng, nếu được chỉ định, được sử dụng làm thông báo lỗi về lỗi (xem thêm

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
67). Lưu ý rằng đối số từ khóa MSG có thể được chuyển đến
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6,
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
69,
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
70,
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
71 chỉ khi chúng được sử dụng làm trình quản lý ngữ cảnh.

________ 50 (thứ nhất, thứ hai, msg = none) ¶(first, second, msg=None)

Kiểm tra rằng thứ nhất và thứ hai là bằng nhau. Nếu các giá trị không so sánh bằng nhau, thử nghiệm sẽ thất bại.

Ngoài ra, nếu thứ nhất và thứ hai là cùng loại chính xác và một danh sách, tuple, dict, set, frozenset hoặc str hoặc bất kỳ loại nào mà một lớp con đăng ký với

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
73 Hàm bình đẳng cụ thể loại sẽ được gọi để tạo ra Thông báo lỗi mặc định hữu ích (xem thêm danh sách các phương thức cụ thể loại).list of type-specific methods).

Đã thay đổi trong phiên bản 3.1: Đã thêm cuộc gọi tự động của hàm bình đẳng cụ thể.Added the automatic calling of type-specific equality function.

Đã thay đổi trong phiên bản 3.2:

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
74 được thêm vào là hàm bình đẳng loại mặc định để so sánh các chuỗi.
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
74 added as the default type equality function for comparing strings.

________ 275 (thứ nhất, thứ hai, msg = none) ¶(first, second, msg=None)

Kiểm tra rằng thứ nhất và thứ hai không bằng nhau. Nếu các giá trị so sánh bằng nhau, bài kiểm tra sẽ thất bại.

________ 276 (expr, msg = none) ____ ____ 277 (expr, msg = none) ¶(expr, msg=None)
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
77(expr, msg=None)

Kiểm tra rằng expr là đúng (hoặc sai).

Lưu ý rằng điều này tương đương với

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
78 và không với
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
79 (sử dụng
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
80 cho cái sau). Phương pháp này cũng nên tránh khi có sẵn các phương pháp cụ thể hơn (ví dụ:
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
43 thay vì
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
82), vì chúng cung cấp thông báo lỗi tốt hơn trong trường hợp thất bại.

________ 283 (thứ nhất, thứ hai, msg = none) ________ 284 (thứ nhất, thứ hai, msg = none) ¶(first, second, msg=None)
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
84(first, second, msg=None)

Kiểm tra rằng thứ nhất và thứ hai là (hoặc không) cùng một đối tượng.

Mới trong phiên bản 3.1.

________ 285 (expr, msg = none) ________ 286 (expr, msg = none) ¶(expr, msg=None)
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
86(expr, msg=None)

Kiểm tra rằng expr là (hoặc không)

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33.

Mới trong phiên bản 3.1.

________ 285 (expr, msg = none) ________ 286 (expr, msg = none) ¶(member, container, msg=None)
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
89(member, container, msg=None)

Kiểm tra rằng expr là (hoặc không)

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33.

Mới trong phiên bản 3.1.

________ 285 (expr, msg = none) ________ 286 (expr, msg = none) ¶(obj, cls, msg=None)
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
91(obj, cls, msg=None)

Kiểm tra rằng expr là (hoặc không)

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33.

________ 288 (Thành viên, Container, MSG = Không) ________ 289 (Thành viên, Container, MSG = Không) ¶

Kiểm tra thành viên đó là (hoặc không) trong container.

Phương pháp

Kiểm tra điều đó

Mới

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
94

Tất cả các phương thức khẳng định chấp nhận một đối số MSG rằng, nếu được chỉ định, được sử dụng làm thông báo lỗi về lỗi (xem thêm

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
67). Lưu ý rằng đối số từ khóa MSG có thể được chuyển đến
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6,
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
69,
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
70,
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
71 chỉ khi chúng được sử dụng làm trình quản lý ngữ cảnh.

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
96

________ 50 (thứ nhất, thứ hai, msg = none) ¶

3.1

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
98

Kiểm tra rằng thứ nhất và thứ hai là bằng nhau. Nếu các giá trị không so sánh bằng nhau, thử nghiệm sẽ thất bại.

3.2

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
00

Ngoài ra, nếu thứ nhất và thứ hai là cùng loại chính xác và một danh sách, tuple, dict, set, frozenset hoặc str hoặc bất kỳ loại nào mà một lớp con đăng ký với

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
73 Hàm bình đẳng cụ thể loại sẽ được gọi để tạo ra Thông báo lỗi mặc định hữu ích (xem thêm danh sách các phương thức cụ thể loại).

3.2

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
02

Đã thay đổi trong phiên bản 3.1: Đã thêm cuộc gọi tự động của hàm bình đẳng cụ thể.

3.4

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
04

Đã thay đổi trong phiên bản 3.2:
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
74 được thêm vào là hàm bình đẳng loại mặc định để so sánh các chuỗi.

________ 275 (thứ nhất, thứ hai, msg = none) ¶

3.10

Kiểm tra rằng thứ nhất và thứ hai không bằng nhau. Nếu các giá trị so sánh bằng nhau, bài kiểm tra sẽ thất bại.(exception, callable, *args, **kwds)
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
06(exception, *, msg=None)

________ 276 (expr, msg = none) ____ ____ 277 (expr, msg = none) ¶

Kiểm tra rằng expr là đúng (hoặc sai).

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
7

Lưu ý rằng điều này tương đương với

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
78 và không với
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
79 (sử dụng
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
80 cho cái sau). Phương pháp này cũng nên tránh khi có sẵn các phương pháp cụ thể hơn (ví dụ:
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
43 thay vì
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
82), vì chúng cung cấp thông báo lỗi tốt hơn trong trường hợp thất bại.

________ 283 (thứ nhất, thứ hai, msg = none) ________ 284 (thứ nhất, thứ hai, msg = none) ¶

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
8

Kiểm tra rằng thứ nhất và thứ hai là (hoặc không) cùng một đối tượng.Added the ability to use

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 as a context manager.

Mới trong phiên bản 3.1.Added the

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
10 attribute.

________ 285 (expr, msg = none) ________ 286 (expr, msg = none) ¶Added the msg keyword argument when used as a context manager.

Kiểm tra rằng expr là (hoặc không)
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33.(exception, regex, callable, *args, **kwds)
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
13(exception, regex, *, msg=None)

________ 288 (Thành viên, Container, MSG = Không) ________ 289 (Thành viên, Container, MSG = Không) ¶

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
9

or:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
0

Kiểm tra thành viên đó là (hoặc không) trong container.Added under the name

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
17.

________ 285 (expr, msg = none) ________ 286 (expr, msg = none) ¶Added the msg keyword argument when used as a context manager.

A(warning, callable, *args, **kwds)
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
18(warning, *, msg=None)

Kiểm tra rằng một cảnh báo được kích hoạt khi có thể gọi được được gọi với bất kỳ đối số vị trí hoặc từ khóa nào cũng được truyền đến

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
70. Bài kiểm tra vượt qua nếu cảnh báo được kích hoạt và thất bại nếu nó không phải là. Bất kỳ ngoại lệ là một lỗi. Để bắt bất kỳ nhóm cảnh báo nào, một tuple chứa các lớp cảnh báo có thể được thông qua dưới dạng cảnh báo.

Nếu chỉ có cảnh báo và có thể đưa ra các đối số MSG, hãy trả về Trình quản lý ngữ cảnh để mã được kiểm tra có thể được ghi nội tuyến chứ không phải là một hàm:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
1

Khi được sử dụng làm trình quản lý ngữ cảnh,

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
70 chấp nhận thông tin từ khóa bổ sung.

Trình quản lý bối cảnh sẽ lưu trữ đối tượng cảnh báo bị bắt trong thuộc tính ____322 của nó và dòng nguồn kích hoạt các cảnh báo trong các thuộc tính

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
23 và
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
24. Điều này có thể hữu ích nếu ý định là thực hiện kiểm tra bổ sung về cảnh báo bị bắt:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
2

Phương pháp này hoạt động bất kể các bộ lọc cảnh báo tại chỗ khi nó được gọi.

Mới trong phiên bản 3.2.

Đã thay đổi trong phiên bản 3.3: Đã thêm đối số từ khóa MSG khi được sử dụng làm trình quản lý ngữ cảnh.Added the msg keyword argument when used as a context manager.

________ 325 (cảnh báo, regex, có thể gọi được, *args, ** kwds)(warning, regex, callable, *args, **kwds)
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
25(warning, regex, *, msg=None)

Giống như

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
70 nhưng cũng kiểm tra rằng Regex khớp với thông điệp của cảnh báo được kích hoạt. Regex có thể là một đối tượng biểu thức chính quy hoặc một chuỗi chứa một biểu thức thông thường phù hợp để sử dụng bởi
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
16. Thí dụ:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
3

or:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
4

Mới trong phiên bản 3.2.

Đã thay đổi trong phiên bản 3.3: Đã thêm đối số từ khóa MSG khi được sử dụng làm trình quản lý ngữ cảnh.Added the msg keyword argument when used as a context manager.

________ 325 (cảnh báo, regex, có thể gọi được, *args, ** kwds)(logger=None, level=None)

Giống như

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
70 nhưng cũng kiểm tra rằng Regex khớp với thông điệp của cảnh báo được kích hoạt. Regex có thể là một đối tượng biểu thức chính quy hoặc một chuỗi chứa một biểu thức thông thường phù hợp để sử dụng bởi
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
16. Thí dụ:

________ 329 (logger = none, level = none) ¶

Trình quản lý bối cảnh để kiểm tra rằng ít nhất một tin nhắn được ghi vào logger hoặc một trong những đứa trẻ của nó, với ít nhất là mức đã cho.

Nếu được đưa ra, logger phải là đối tượng

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
30 hoặc
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
31 cho tên của một logger. Mặc định là logger gốc, sẽ bắt tất cả các tin nhắn không bị chặn bởi một logger hạ nguồn không lưu trữ.

Nếu được đưa ra, mức phải là mức ghi nhật ký số hoặc chuỗi tương đương của nó (ví dụ:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
32 hoặc
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
33). Mặc định là
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
34.

Bài kiểm tra truyền nếu ít nhất một thông báo phát ra bên trong khối
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
03 phù hợp với các điều kiện logger và cấp độ, nếu không nó sẽ thất bại.

Đối tượng được trả về bởi Trình quản lý ngữ cảnh là một trình trợ giúp ghi âm giữ các bản nhạc của các thông báo nhật ký phù hợp. Nó có hai thuộc tính:

________ 336¶

Danh sách các đối tượng

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
37 của các thông báo nhật ký phù hợp.

Example:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
5

________ 338¶

Một danh sách các đối tượng
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
31 với đầu ra được định dạng của các tin nhắn phù hợp.(logger=None, level=None)

Mới trong phiên bản 3.4.

________ 340 (logger = none, level = none) ¶

Trình quản lý bối cảnh để kiểm tra rằng ít nhất một tin nhắn được ghi vào logger hoặc một trong những đứa trẻ của nó, với ít nhất là mức đã cho.

Nếu được đưa ra, logger phải là đối tượng

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
30 hoặc
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
31 cho tên của một logger. Mặc định là logger gốc, sẽ bắt tất cả các tin nhắn không bị chặn bởi một logger hạ nguồn không lưu trữ.

Nếu được đưa ra, mức phải là mức ghi nhật ký số hoặc chuỗi tương đương của nó (ví dụ:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
32 hoặc
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
33). Mặc định là
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
34.

Bài kiểm tra truyền nếu ít nhất một thông báo phát ra bên trong khối

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
03 phù hợp với các điều kiện logger và cấp độ, nếu không nó sẽ thất bại.

Đối tượng được trả về bởi Trình quản lý ngữ cảnh là một trình trợ giúp ghi âm giữ các bản nhạc của các thông báo nhật ký phù hợp. Nó có hai thuộc tính:

________ 336¶

Danh sách các đối tượng

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
37 của các thông báo nhật ký phù hợp.

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
47

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
48

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
49

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
50

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
51

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
52

3.1

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
53

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
54

3.1

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
55

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
56

3.1

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
57

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
58

3.1

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
59

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
60

3.1

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
61

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
62

3.2

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
63

________ 338¶

3.2

Một danh sách các đối tượng
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
31 với đầu ra được định dạng của các tin nhắn phù hợp.(first, second, places=7, msg=None, delta=None)
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
65(first, second, places=7, msg=None, delta=None)

Mới trong phiên bản 3.4.

________ 340 (logger = none, level = none) ¶

Trình quản lý bối cảnh để kiểm tra rằng không có tin nhắn nào được ghi vào logger hoặc một trong những đứa trẻ của nó, với ít nhất là mức đã cho.

Nếu được đưa ra, logger phải là đối tượng

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
30 hoặc
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
31 cho tên của một logger. Mặc định là logger gốc, sẽ bắt tất cả các tin nhắn.
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
68 automatically considers almost equal objects that compare equal.
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
69 automatically fails if the objects compare equal. Added the delta keyword argument.

Không giống như
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
46, sẽ không có gì được trả về bởi người quản lý bối cảnh.(first, second, msg=None)
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
71(first, second, msg=None)
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
72(first, second, msg=None)
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
73(first, second, msg=None)

Kiểm tra lần đầu tiên là tương ứng>,> =,

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
6

Mới trong phiên bản 3.1.

________ 374 (văn bản, regex, msg = none)(text, regex, msg=None)
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
75(text, regex, msg=None)

Kiểm tra rằng một tìm kiếm regex phù hợp với văn bản (hoặc không khớp). Trong trường hợp thất bại, thông báo lỗi sẽ bao gồm mẫu và văn bản (hoặc mẫu và một phần của văn bản không ngờ tới). Regex có thể là một đối tượng biểu thức chính quy hoặc một chuỗi chứa một biểu thức thông thường phù hợp để sử dụng bởi

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
16.

Mới trong phiên bản 3.1: Được thêm vào dưới tên

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
77.Added under the name
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
77.

Thay đổi trong phiên bản 3.2: Phương pháp

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
78 đã được đổi tên thành
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
79.The method
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
78 has been renamed to
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
79.

Mới trong phiên bản 3.5: Tên

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
80 là bí danh không dùng nữa cho
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
81.The name
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
80 is a deprecated alias for
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
81.

________ 382 (thứ nhất, thứ hai, msg = none) ¶(first, second, msg=None)

Kiểm tra trình tự đầu tiên chứa các yếu tố giống như thứ hai, bất kể thứ tự của chúng. Khi họ don, một thông báo lỗi liệt kê sự khác biệt giữa các chuỗi sẽ được tạo ra.

Các yếu tố trùng lặp không bị bỏ qua khi so sánh thứ nhất và thứ hai. Nó xác minh xem mỗi phần tử có cùng số lượng trong cả hai chuỗi hay không. Tương đương với:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
83 nhưng cũng hoạt động với các chuỗi các đối tượng không thể chối cãi.

Mới trong phiên bản 3.2.

Phương pháp

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
3 gửi kiểm tra bình đẳng cho các đối tượng cùng loại cho các phương thức cụ thể loại khác nhau. Các phương pháp này đã được triển khai cho hầu hết các loại tích hợp, nhưng nó cũng có thể đăng ký các phương thức mới bằng cách sử dụng
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
73:

________ 386 (typeObj, chức năng) ¶(typeobj, function)

Đăng ký một phương thức cụ thể loại được gọi bởi

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
3 để kiểm tra xem hai đối tượng có cùng loại cùng loại (không phải các lớp con) so sánh bằng nhau không. Hàm phải lấy hai đối số vị trí và MSG thứ ba = không có đối số từ khóa giống như
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
3. Nó phải tăng
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
89 khi sự bất bình đẳng giữa hai tham số đầu tiên được phát hiện - có thể cung cấp thông tin hữu ích và giải thích sự bất bình đẳng chi tiết trong thông báo lỗi.

Mới trong phiên bản 3.1.

Danh sách các phương thức cụ thể loại tự động được sử dụng bởi

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
3 được tóm tắt trong bảng sau. Lưu ý rằng nó thường không cần thiết để gọi các phương pháp này trực tiếp.

Phương pháp

Được sử dụng để so sánh

Mới

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
91

dây

3.1

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
92

trình tự

3.1

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
93

danh sách

3.1

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
94

bộ dữ liệu

3.1

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
95

bộ hoặc đông lạnh

3.1

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
96

chỉ đạo

3.1

________ 397 (thứ nhất, thứ hai, msg = none) ¶(first, second, msg=None)

Kiểm tra rằng chuỗi đa dòng trước tiên bằng với chuỗi thứ hai. Khi không bằng một sự khác biệt của hai chuỗi làm nổi bật sự khác biệt sẽ được bao gồm trong thông báo lỗi. Phương pháp này được sử dụng theo mặc định khi so sánh các chuỗi với

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
3.

Mới trong phiên bản 3.1.

________ 399 (thứ nhất, thứ hai, msg = none, seq_type = none) ¶(first, second, msg=None, seq_type=None)

Các thử nghiệm rằng hai chuỗi là bằng nhau. Nếu SEQ_TYPE được cung cấp, cả thứ nhất và thứ hai phải là các trường hợp của SEQ_TYPE hoặc một lỗi sẽ được nêu ra. Nếu các chuỗi khác nhau, một thông báo lỗi được xây dựng cho thấy sự khác biệt giữa hai thông báo.

Phương pháp này không được gọi trực tiếp bởi

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
3, nhưng nó được sử dụng để thực hiện
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
01 và
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
02.

Mới trong phiên bản 3.1.

________ 403 (thứ nhất, thứ hai, msg = none) ________ 404 (thứ nhất, thứ hai, msg = none) ¶(first, second, msg=None)
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
04(first, second, msg=None)

Các thử nghiệm rằng hai danh sách hoặc bộ dữ liệu là bằng nhau. Nếu không, một thông báo lỗi được xây dựng chỉ hiển thị sự khác biệt giữa hai thông báo. Một lỗi cũng được nêu ra nếu một trong hai tham số thuộc loại sai. Các phương pháp này được sử dụng theo mặc định khi so sánh danh sách hoặc bộ dữ liệu với

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
3.

Mới trong phiên bản 3.1.

________ 406 (thứ nhất, thứ hai, msg = none) ¶(first, second, msg=None)

Các thử nghiệm rằng hai bộ là bằng nhau. Nếu không, một thông báo lỗi được xây dựng liệt kê sự khác biệt giữa các bộ. Phương pháp này được sử dụng theo mặc định khi so sánh các bộ hoặc đông lạnh với

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
3.

Thất bại nếu một trong hai hoặc thứ hai không có phương thức

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
08.

Mới trong phiên bản 3.1.

________ 51 (thứ nhất, thứ hai, msg = none) ¶(first, second, msg=None)

Kiểm tra rằng hai từ điển là bằng nhau. Nếu không, một thông báo lỗi được xây dựng cho thấy sự khác biệt trong từ điển. Phương pháp này sẽ được sử dụng theo mặc định để so sánh từ điển trong các cuộc gọi đến

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
3.

Mới trong phiên bản 3.1.

Cuối cùng,

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 cung cấp các phương thức và thuộc tính sau:

________ 412 (msg = none) ¶(msg=None)

Báo hiệu lỗi kiểm tra vô điều kiện, với MSG hoặc

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33 cho thông báo lỗi.

________ 414¶

Thuộc tính lớp này đưa ra ngoại lệ được nâng lên bởi phương thức kiểm tra. Nếu một khung kiểm tra cần sử dụng một ngoại lệ chuyên dụng, có thể mang thêm thông tin, nó phải phân lớp ngoại lệ này để chơi trò chơi công bằng với khung. Giá trị ban đầu của thuộc tính này là

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
19.

________ 416¶

Thuộc tính lớp này xác định những gì xảy ra khi một thông báo thất bại tùy chỉnh được truyền khi đối số MSG cho một cuộc gọi Assertxyy không thành công.

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
17 là giá trị mặc định. Trong trường hợp này, thông báo tùy chỉnh được thêm vào cuối thông báo lỗi tiêu chuẩn. Khi được đặt thành
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
18, thông báo tùy chỉnh sẽ thay thế thông báo tiêu chuẩn.

Cài đặt lớp có thể được ghi đè trong các phương thức thử nghiệm riêng lẻ bằng cách gán một thuộc tính thể hiện, self.LongMessage, cho

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
17 hoặc
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
18 trước khi gọi các phương thức Assert.

Cài đặt lớp được đặt lại trước mỗi cuộc gọi kiểm tra.

Mới trong phiên bản 3.1.

________ 421¶

Thuộc tính này kiểm soát độ dài tối đa của đầu ra Diffs bằng các phương thức khẳng định báo cáo khác với lỗi. Nó mặc định là 80*8 ký tự. Các phương thức khẳng định bị ảnh hưởng bởi thuộc tính này là

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
22 (bao gồm tất cả các phương pháp so sánh trình tự ủy thác cho nó),
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
23 và
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
74.

Cài đặt

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
25 thành
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33 có nghĩa là không có độ dài tối đa của các khác.

Mới trong phiên bản 3.2.

Khung kiểm tra có thể sử dụng các phương pháp sau để thu thập thông tin trong bài kiểm tra:

________ 427 ()()

Trả về số lượng thử nghiệm được biểu thị bởi đối tượng thử nghiệm này. Đối với các trường hợp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5, điều này sẽ luôn là
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
29.

________ 430 ()()

Trả về một thể hiện của lớp kết quả kiểm tra nên được sử dụng cho lớp trường hợp thử nghiệm này (nếu không có phiên bản kết quả nào khác được cung cấp cho phương pháp

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
35).

Đối với các trường hợp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5, đây sẽ luôn là một ví dụ của
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76; Các lớp con của
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 sẽ ghi đè lên điều này khi cần thiết.

________ 435 ()()

Trả về một chuỗi xác định trường hợp kiểm tra cụ thể. Đây thường là tên đầy đủ của phương pháp kiểm tra, bao gồm mô -đun và tên lớp.

________ 436 ()()

Trả về mô tả của bài kiểm tra, hoặc

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33 nếu không có mô tả nào được cung cấp. Việc triển khai mặc định của phương thức này trả về dòng đầu tiên của phương thức kiểm tra DocString, nếu có hoặc
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33.

Đã thay đổi trong phiên bản 3.1: Trong 3.1, điều này đã được thay đổi để thêm tên thử nghiệm vào mô tả ngắn ngay cả khi có mặt của một tài liệu. Điều này gây ra các vấn đề tương thích với các tiện ích mở rộng nhất quán và thêm tên thử nghiệm đã được chuyển đến

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
39 trong Python 3.2.In 3.1 this was changed to add the test name to the short description even in the presence of a docstring. This caused compatibility issues with unittest extensions and adding the test name was moved to the
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
39 in Python 3.2.

________ 440 (chức năng, /, *args, ** kwargs) ¶(function, /, *args, **kwargs)

Thêm một chức năng để được gọi theo sau

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 để dọn dẹp tài nguyên được sử dụng trong quá trình thử nghiệm. Các chức năng sẽ được gọi theo thứ tự ngược lại theo thứ tự chúng được thêm vào (LIFO). Chúng được gọi với bất kỳ đối số và từ khóa nào được truyền vào
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
42 khi chúng được thêm vào.

Nếu

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
8 không thành công, có nghĩa là
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 không được gọi, thì bất kỳ chức năng dọn dẹp nào được thêm vào vẫn sẽ được gọi.

Mới trong phiên bản 3.1.

________ 421¶()

Thuộc tính này kiểm soát độ dài tối đa của đầu ra Diffs bằng các phương thức khẳng định báo cáo khác với lỗi. Nó mặc định là 80*8 ký tự. Các phương thức khẳng định bị ảnh hưởng bởi thuộc tính này là

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
22 (bao gồm tất cả các phương pháp so sánh trình tự ủy thác cho nó),
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
23 và
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
74.

Cài đặt

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
25 thành
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33 có nghĩa là không có độ dài tối đa của các khác.

Mới trong phiên bản 3.2.

Mới trong phiên bản 3.1.

Khung kiểm tra có thể sử dụng các phương pháp sau để thu thập thông tin trong bài kiểm tra:(function, /, *args, **kwargs)

________ 427 ()

Trả về số lượng thử nghiệm được biểu thị bởi đối tượng thử nghiệm này. Đối với các trường hợp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5, điều này sẽ luôn là
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
29.

________ 430 ()

Trả về một thể hiện của lớp kết quả kiểm tra nên được sử dụng cho lớp trường hợp thử nghiệm này (nếu không có phiên bản kết quả nào khác được cung cấp cho phương pháp
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
35).()

Đối với các trường hợp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5, đây sẽ luôn là một ví dụ của
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76; Các lớp con của
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 sẽ ghi đè lên điều này khi cần thiết.

________ 435 ()

Trả về một chuỗi xác định trường hợp kiểm tra cụ thể. Đây thường là tên đầy đủ của phương pháp kiểm tra, bao gồm mô -đun và tên lớp.

________ 430 ()

Trả về một thể hiện của lớp kết quả kiểm tra nên được sử dụng cho lớp trường hợp thử nghiệm này (nếu không có phiên bản kết quả nào khác được cung cấp cho phương pháp
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
35).(methodName='runTest')

Đối với các trường hợp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5, đây sẽ luôn là một ví dụ của
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76; Các lớp con của
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 sẽ ghi đè lên điều này khi cần thiết.

________ 430 ()

Trả về một thể hiện của lớp kết quả kiểm tra nên được sử dụng cho lớp trường hợp thử nghiệm này (nếu không có phiên bản kết quả nào khác được cung cấp cho phương pháp
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
35).()

Đối với các trường hợp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5, đây sẽ luôn là một ví dụ của
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76; Các lớp con của
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 sẽ ghi đè lên điều này khi cần thiết.

________ 435 ()()

Trả về một chuỗi xác định trường hợp kiểm tra cụ thể. Đây thường là tên đầy đủ của phương pháp kiểm tra, bao gồm mô -đun và tên lớp.

________ 478 (chức năng, /, *args, ** kwargs) ¶(function, /, *args, **kwargs)

Phương pháp này chấp nhận một coroutine có thể được sử dụng như một hàm làm sạch.

________ 231 (result = none) ¶(result=None)

Đặt một vòng lặp sự kiện mới để chạy thử nghiệm, thu thập kết quả vào đối tượng

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76 được truyền qua kết quả. Nếu kết quả bị bỏ qua hoặc
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33, một đối tượng kết quả tạm thời được tạo (bằng cách gọi phương thức
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
34) và được sử dụng. Đối tượng kết quả được trả lại cho người gọi ____ 235. Vào cuối bài kiểm tra, tất cả các nhiệm vụ trong vòng lặp sự kiện bị hủy bỏ.

Một ví dụ minh họa thứ tự:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
7

Sau khi chạy thử nghiệm,

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
84 sẽ chứa
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
85.

Lớp ________ 185 ________ 487 (testfunc, setup = none, refown = none, description = none) ¶(testFunc, setUp=None, tearDown=None, description=None)

Lớp này thực hiện phần của giao diện

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 cho phép người chạy thử nghiệm lái thử, nhưng không cung cấp các phương thức mà mã kiểm tra có thể sử dụng để kiểm tra và báo cáo lỗi. Điều này được sử dụng để tạo các trường hợp thử nghiệm bằng cách sử dụng mã thử nghiệm cũ, cho phép nó được tích hợp vào khung thử nghiệm dựa trên ________ 52.

Bí danh không dùng nữa

Vì lý do lịch sử, một số phương pháp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 có một hoặc nhiều bí danh hiện đang bị phản đối. Bảng sau đây liệt kê các tên chính xác cùng với các bí danh không dùng nữa của chúng:

Bài kiểm tra nhóm

Lớp ________ 185 ________ 492 (Test = ())(tests=())

Lớp này đại diện cho một tập hợp các trường hợp thử nghiệm và bộ thử nghiệm riêng lẻ. Lớp trình bày giao diện cần thiết cho người chạy thử để cho phép nó được chạy như bất kỳ trường hợp kiểm tra nào khác. Chạy một thể hiện

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59 giống như lặp lại trên bộ, chạy từng bài kiểm tra riêng lẻ.

Nếu các thử nghiệm được đưa ra, nó phải là một trường hợp thử nghiệm riêng lẻ hoặc các bộ thử nghiệm khác sẽ được sử dụng để xây dựng bộ ban đầu. Các phương pháp bổ sung được cung cấp để thêm các trường hợp thử nghiệm và bộ cho bộ sưu tập sau này.

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59 Các đối tượng hoạt động giống như các đối tượng
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5, ngoại trừ chúng không thực sự thực hiện thử nghiệm. Thay vào đó, chúng được sử dụng để tổng hợp các bài kiểm tra thành các nhóm thử nghiệm nên được chạy cùng nhau. Một số phương pháp bổ sung có sẵn để thêm các thử nghiệm vào các trường hợp
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59:

________ 497 (kiểm tra) ¶(test)

Thêm

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 hoặc
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59 vào bộ.

________ 500 (Bài kiểm tra) ¶(tests)

Thêm tất cả các thử nghiệm từ một trường hợp có thể lặp lại của các trường hợp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 và
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59 vào bộ thử nghiệm này.

Điều này tương đương với việc lặp lại qua các bài kiểm tra, gọi

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
03 cho mỗi yếu tố.

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59 chia sẻ các phương pháp sau với
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5:

________ 231 (kết quả) ¶(result)

Chạy các bài kiểm tra liên quan đến bộ này, thu thập kết quả vào đối tượng kết quả kiểm tra được truyền qua kết quả. Lưu ý rằng không giống như

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
07,
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
08 yêu cầu đối tượng kết quả được truyền vào.

________ 241 ()()

Chạy các bài kiểm tra liên quan đến bộ này mà không thu thập kết quả. Điều này cho phép các ngoại lệ được đưa ra bởi bài kiểm tra được truyền đến người gọi và có thể được sử dụng để hỗ trợ các thử nghiệm chạy theo trình gỡ lỗi.

________ 427 ()()

Trả về số lượng thử nghiệm được biểu thị bởi đối tượng thử nghiệm này, bao gồm tất cả các bài kiểm tra và bộ phụ riêng lẻ.

________ 511 ()()

Các thử nghiệm được nhóm bởi

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59 luôn được truy cập bằng cách lặp. Các lớp con có thể cung cấp các bài kiểm tra một cách lười biếng bằng cách ghi đè
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
13. Lưu ý rằng phương pháp này có thể được gọi nhiều lần trên một bộ (ví dụ: khi đếm các bài kiểm tra hoặc so sánh cho bình đẳng) để các thử nghiệm được trả về bằng các lần lặp lặp lại trước khi
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
08 phải giống nhau cho mỗi lần lặp lại. Sau
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
08, người gọi không nên dựa vào các bài kiểm tra được trả về bằng phương thức này trừ khi người gọi sử dụng một lớp con ghi đè
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
16 để lưu giữ các tài liệu tham khảo thử nghiệm.

Đã thay đổi trong phiên bản 3.2: Trong các phiên bản trước, các bài kiểm tra được truy cập trực tiếp thay vì thông qua việc lặp lại, do đó, ghi đè

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
13 không đủ để cung cấp các bài kiểm tra.In earlier versions the
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59 accessed tests directly rather than through iteration, so overriding
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
13 wasn’t sufficient for providing tests.

Đã thay đổi trong phiên bản 3.4: Trong các phiên bản trước,

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59 đã tổ chức các tài liệu tham khảo cho mỗi
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 sau
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
08. Các lớp con có thể khôi phục hành vi đó bằng cách ghi đè
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
16.In earlier versions the
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59 held references to each
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 after
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
08. Subclasses can restore that behavior by overriding
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
16.

Trong cách sử dụng điển hình của đối tượng

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59, phương pháp
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
35 được gọi bởi
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
25 chứ không phải bởi khai thác thử nghiệm của người dùng cuối.

Đang tải và chạy thử nghiệm

Lớp ________ 185 ________ 527¶

Lớp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
28 được sử dụng để tạo các bộ thử nghiệm từ các lớp và mô -đun. Thông thường, không cần phải tạo một thể hiện của lớp này; Mô -đun
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2 cung cấp một thể hiện có thể được chia sẻ là
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
30. Tuy nhiên, sử dụng một lớp con hoặc thể hiện cho phép tùy chỉnh một số thuộc tính có thể định cấu hình.

Đối tượng

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
28 có các thuộc tính sau:

________ 532¶

Một danh sách các lỗi không gây tử vong gặp phải trong khi tải các bài kiểm tra. Không đặt lại bởi trình tải tại bất kỳ điểm nào. Các lỗi gây tử vong được báo hiệu bằng phương pháp có liên quan nâng cao ngoại lệ cho người gọi. Các lỗi không gây tử vong cũng được biểu thị bằng một bài kiểm tra tổng hợp sẽ làm tăng lỗi ban đầu khi chạy.

Mới trong phiên bản 3.5.

Đối tượng

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
28 có các phương pháp sau:

________ 534 (testcaseclass) ¶(testCaseClass)

Trả về một bộ của tất cả các trường hợp thử nghiệm có trong

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
36 có nguồn gốc từ ____ 55.

Một trường hợp thử nghiệm được tạo cho mỗi phương thức được đặt tên bởi

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
37. Theo mặc định, đây là các tên phương thức bắt đầu bằng
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
2. Nếu
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
37 trả về không có phương thức, nhưng phương thức
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
14 được thực hiện, một trường hợp thử nghiệm duy nhất được tạo cho phương thức đó thay thế.

________ 541 (Mô -đun, Mẫu = Không) ¶(module, pattern=None)

Trả về một bộ của tất cả các trường hợp thử nghiệm có trong mô -đun đã cho. Phương pháp này tìm kiếm mô -đun cho các lớp có nguồn gốc từ

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 và tạo một thể hiện của lớp cho mỗi phương thức kiểm tra được xác định cho lớp.

Ghi chú

Mặc dù sử dụng hệ thống phân cấp của các lớp có nguồn gốc ____ 55 có thể thuận tiện trong việc chia sẻ đồ đạc và chức năng trợ giúp, việc xác định các phương thức kiểm tra trên các lớp cơ sở không nhằm mục đích trực tiếp không chơi tốt với phương pháp này. Làm như vậy, tuy nhiên, có thể hữu ích khi các đồ đạc khác nhau và được xác định trong các lớp con.

Nếu một mô -đun cung cấp chức năng

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44, nó sẽ được gọi để tải các thử nghiệm. Điều này cho phép các mô -đun tùy chỉnh tải thử. Đây là giao thức load_tests. Đối số mẫu được truyền như là đối số thứ ba cho
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44.

Đã thay đổi trong phiên bản 3.2: Hỗ trợ cho

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44 được thêm vào.Support for
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44 added.

Đã thay đổi trong phiên bản 3.5: đối số mặc định không chính thức và không chính thức_Load_Tests không được chấp nhận và bỏ qua, mặc dù nó vẫn được chấp nhận để tương thích ngược. Phương thức hiện cũng chấp nhận một mẫu đối số chỉ dành cho từ khóa được chuyển đến

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44 làm đối số thứ ba.The undocumented and unofficial use_load_tests default argument is deprecated and ignored, although it is still accepted for backward compatibility. The method also now accepts a keyword-only argument pattern which is passed to
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44 as the third argument.

________ 548 (Tên, Mô -đun = Không) ¶(name, module=None)

Trả về một bộ của tất cả các trường hợp thử nghiệm được đưa ra một chỉ định chuỗi.

Tên trình xác định là một tên chấm chấm của người Viking có thể giải quyết thành một mô -đun, lớp trường hợp thử nghiệm, phương thức kiểm tra trong lớp trường hợp thử nghiệm, phiên bản

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59 hoặc đối tượng có thể gọi được trả về một phiên bản
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 hoặc
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59. Các kiểm tra này được áp dụng trong đơn đặt hàng được liệt kê ở đây; Đó là, một phương pháp trên một lớp trường hợp thử nghiệm có thể sẽ được chọn là một phương pháp thử nghiệm trong một trường hợp thử nghiệm, thay vì một đối tượng có thể gọi được.

Ví dụ: nếu bạn có mô-đun

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
52 có chứa lớp ____ 55 có nguồn gốc
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
54 với ba phương pháp thử nghiệm (
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
55,
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
56 và
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
57), trình xác nhận
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
58 sẽ khiến phương thức này trả về một bộ sẽ chạy tất cả ba phương pháp thử nghiệm. Sử dụng bộ xác định
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
59 sẽ khiến nó trả về bộ thử nghiệm sẽ chỉ chạy phương pháp kiểm tra
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
56. Bộ xác định có thể tham khảo các mô -đun và gói chưa được nhập; Chúng sẽ được nhập khẩu dưới dạng tác dụng phụ.

Phương pháp tùy chọn giải quyết tên so với mô -đun đã cho.

Đã thay đổi trong phiên bản 3.5: Nếu xảy ra

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
61 hoặc
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
62 trong khi đi qua tên thì một bài kiểm tra tổng hợp làm tăng lỗi đó khi chạy sẽ được trả về. Những lỗi này được bao gồm trong các lỗi được tích lũy bởi self.errors.If an
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
61 or
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
62 occurs while traversing name then a synthetic test that raises that error when run will be returned. These errors are included in the errors accumulated by self.errors.

________ 563 (tên, mô -đun = không) ¶(names, module=None)

Tương tự như

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
64, nhưng có một chuỗi các tên chứ không phải là một tên duy nhất. Giá trị trả về là một bộ thử nghiệm hỗ trợ tất cả các thử nghiệm được xác định cho mỗi tên.

________ 565 (testcaseclass) ¶(testCaseClass)

Trả về một chuỗi được sắp xếp các tên phương thức được tìm thấy trong testcaseclass; Đây phải là một lớp con của

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5.

________ 567 (start_dir, catplay = 'test*.py', top_level_dir = none) ¶(start_dir, pattern='test*.py', top_level_dir=None)

Tìm tất cả các mô -đun thử nghiệm bằng cách đệ quy thành các thư mục con từ thư mục bắt đầu được chỉ định và trả về một đối tượng TestSuite chứa chúng. Chỉ các tệp kiểm tra mà mẫu khớp sẽ được tải. .

Tất cả các mô -đun thử nghiệm phải được nhập từ cấp cao nhất của dự án. Nếu thư mục bắt đầu không phải là thư mục cấp cao nhất thì thư mục cấp cao nhất phải được chỉ định riêng.

Nếu nhập một mô -đun không thành công, ví dụ do lỗi cú pháp, thì điều này sẽ được ghi lại dưới dạng một lỗi duy nhất và khám phá sẽ tiếp tục. Nếu lỗi nhập là do

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
80 được nâng lên, nó sẽ được ghi lại dưới dạng bỏ qua thay vì lỗi.

Nếu một gói (một thư mục chứa một tệp có tên

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
69) được tìm thấy, gói sẽ được kiểm tra chức năng
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44. Nếu điều này tồn tại thì nó sẽ được gọi là
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
71. Thử nghiệm khám phá cẩn thận để đảm bảo rằng gói chỉ được kiểm tra các thử nghiệm một lần trong quá trình gọi, ngay cả khi chức năng Load_Tests tự gọi
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
72.

Nếu

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44 tồn tại thì Discovery không tái phát vào gói,
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44 chịu trách nhiệm tải tất cả các thử nghiệm trong gói.

Mẫu được cố tình không được lưu trữ dưới dạng thuộc tính Trình tải để các gói có thể tiếp tục tự khám phá. TOP_LEVEL_DIR được lưu trữ để

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44 không cần chuyển đối số này trong
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
76.

start_dir có thể là một tên mô -đun chấm cũng như một thư mục.

Mới trong phiên bản 3.2.

Thay đổi trong phiên bản 3.4: Các mô -đun tăng

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
80 khi nhập được ghi là bỏ qua, không phải lỗi.Modules that raise
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
80 on import are recorded as skips, not errors.

Đã thay đổi trong phiên bản 3.4: Các đường dẫn được sắp xếp trước khi được nhập để lệnh thực thi giống nhau ngay cả khi hệ thống tệp cơ bản đặt hàng không phụ thuộc vào tên tệp.Paths are sorted before being imported so that execution order is the same even if the underlying file system’s ordering is not dependent on file name.

Đã thay đổi trong phiên bản 3.5: Các gói được tìm thấy hiện đã được kiểm tra

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44 bất kể đường dẫn của chúng có khớp với mẫu hay không, bởi vì tên gói không thể khớp với mẫu mặc định.Found packages are now checked for
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44 regardless of whether their path matches pattern, because it is impossible for a package name to match the default pattern.

Các thuộc tính sau của

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
28 có thể được cấu hình bằng cách phân lớp hoặc gán trên một thể hiện:

________ 580¶

Chuỗi đặt cho tiền tố của các tên phương thức sẽ được hiểu là các phương thức kiểm tra. Giá trị mặc định là

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
81.

Điều này ảnh hưởng đến

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
37 và tất cả các phương pháp
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
83.

________ 584¶

Chức năng được sử dụng để so sánh các tên phương thức khi sắp xếp chúng trong

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
37 và tất cả các phương thức
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
83.

________ 587¶

Đối tượng có thể gọi được xây dựng một bộ thử nghiệm từ một danh sách các bài kiểm tra. Không có phương pháp trên đối tượng kết quả là cần thiết. Giá trị mặc định là lớp

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59.

Điều này ảnh hưởng đến tất cả các phương pháp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
83.

________ 590¶

Danh sách các mẫu tên thử nghiệm ký tự đại diện theo kiểu shell shell phải phù hợp để được đưa vào các bộ thử nghiệm (xem tùy chọn

python -m unittest tests/test_something.py
1).

Nếu thuộc tính này không phải là

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33 (mặc định), tất cả các phương thức thử nghiệm được đưa vào trong các bộ thử nghiệm phải khớp với một trong các mẫu trong danh sách này. Lưu ý rằng các trận đấu luôn được thực hiện bằng cách sử dụng
cd project_directory
python -m unittest discover
3, do đó, không giống như các mẫu được truyền đến tùy chọn
python -m unittest tests/test_something.py
1, các mẫu con đơn giản sẽ phải được chuyển đổi bằng cách sử dụng các ký tự đại diện ____92.

Điều này ảnh hưởng đến tất cả các phương pháp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
83.

________ 590¶

Danh sách các mẫu tên thử nghiệm ký tự đại diện theo kiểu shell shell phải phù hợp để được đưa vào các bộ thử nghiệm (xem tùy chọn
python -m unittest tests/test_something.py
1).

Nếu thuộc tính này không phải là

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33 (mặc định), tất cả các phương thức thử nghiệm được đưa vào trong các bộ thử nghiệm phải khớp với một trong các mẫu trong danh sách này. Lưu ý rằng các trận đấu luôn được thực hiện bằng cách sử dụng
cd project_directory
python -m unittest discover
3, do đó, không giống như các mẫu được truyền đến tùy chọn
python -m unittest tests/test_something.py
1, các mẫu con đơn giản sẽ phải được chuyển đổi bằng cách sử dụng các ký tự đại diện ____92.

Mới trong phiên bản 3.7.

Lớp ________ 185 ________ 598¶

Lớp này được sử dụng để biên dịch thông tin về những bài kiểm tra đã thành công và đã thất bại.

Đối tượng
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76 lưu trữ kết quả của một tập hợp các thử nghiệm. Các lớp
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 và
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59 đảm bảo rằng kết quả được ghi lại đúng cách; Các tác giả kiểm tra không cần phải lo lắng về việc ghi lại kết quả của các bài kiểm tra.

Các khung thử nghiệm được xây dựng trên đầu

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2 có thể muốn truy cập vào đối tượng
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76 được tạo bằng cách chạy một tập hợp các thử nghiệm cho mục đích báo cáo; Một ví dụ
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76 được trả về bằng phương pháp
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
05 cho mục đích này.

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76 Các trường hợp có các thuộc tính sau sẽ được quan tâm khi kiểm tra kết quả chạy một tập hợp các bài kiểm tra:

________ 532¶

Một danh sách chứa 2 bộ của các trường hợp
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 và các chuỗi giữ các dấu vết được định dạng. Mỗi tuple đại diện cho một bài kiểm tra làm tăng một ngoại lệ bất ngờ.

________ 609¶

Một danh sách chứa 2 bộ của các trường hợp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 và các chuỗi giữ các dấu vết được định dạng. Mỗi tuple đại diện cho một thử nghiệm trong đó một lỗi được báo hiệu rõ ràng bằng các phương pháp
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
11.

________ 612¶

Một danh sách chứa 2 bộ của các trường hợp và chuỗi

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 giữ lý do bỏ qua bài kiểm tra.

Mới trong phiên bản 3.1.

________ 614¶

Một danh sách chứa 2 bộ của các trường hợp
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 và các chuỗi giữ các dấu vết được định dạng. Mỗi tuple đại diện cho một lỗi hoặc lỗi dự kiến ​​của trường hợp kiểm tra.

________ 616¶

Một danh sách chứa các trường hợp
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 được đánh dấu là những thất bại dự kiến, nhưng đã thành công.

________ 618¶

Được đặt thành
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
17 khi việc thực hiện các bài kiểm tra sẽ dừng lại bằng
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
20.

________ 621¶

Mới trong phiên bản 3.2.

Tổng số bài kiểm tra chạy cho đến nay.

________ 622¶

Mới trong phiên bản 3.2.

Nếu được đặt thành true,
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
23 và
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
24 sẽ được đệm ở giữa
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
25 và
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
26 được gọi. Đầu ra được thu thập sẽ chỉ được lặp lại trên real
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
23 và
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
24 nếu thử nghiệm không thành công hoặc lỗi. Bất kỳ đầu ra cũng được gắn vào thông báo lỗi / lỗi.

________ 629¶

Nếu được đặt thành true

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
20 sẽ được gọi vào lỗi hoặc lỗi đầu tiên, tạm dừng lần chạy thử.

________ 631¶()

Nếu được đặt thành True thì các biến cục bộ sẽ được hiển thị trong Tracebacks.

Mới trong phiên bản 3.5.()

________ 632 ()

Trả về

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
17 Nếu tất cả các thử nghiệm chạy cho đến nay đã vượt qua, nếu không, sẽ trả về
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
18.

Các phương pháp sau đây của lớp

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76 được sử dụng để duy trì các cấu trúc dữ liệu nội bộ và có thể được mở rộng trong các lớp con để hỗ trợ các yêu cầu báo cáo bổ sung. Điều này đặc biệt hữu ích trong việc xây dựng các công cụ hỗ trợ báo cáo tương tác trong khi các thử nghiệm đang được chạy.

________ 642 (kiểm tra) ¶(test)

Được gọi khi thử nghiệm thử nghiệm sắp được chạy.

________ 643 (kiểm tra) ¶(test)

Được gọi sau khi thử nghiệm thử nghiệm đã được thực hiện, bất kể kết quả.

________ 644 ()()

Được gọi một lần trước khi bất kỳ bài kiểm tra được thực thi.

Mới trong phiên bản 3.1.

________ 645 ()()

Được gọi một lần sau khi tất cả các bài kiểm tra được thực hiện.

Mới trong phiên bản 3.1.

________ 645 ()(test, err)

Được gọi một lần sau khi tất cả các bài kiểm tra được thực hiện.

________ 646 (Kiểm tra, ERR) ¶

Được gọi khi thử nghiệm thử nghiệm làm tăng một ngoại lệ bất ngờ. ERR là một tuple của biểu mẫu được trả về bởi
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
47:
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
48.(test, err)

Việc triển khai mặc định nối thêm một Tuple

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
49 vào thuộc tính của thuộc tính ____ ____650, trong đó định dạng_err là một dấu vết được định dạng có được từ ERR.

________ 651 (Kiểm tra, ERR) ¶

Được gọi khi trường hợp thử nghiệm báo hiệu một lỗi. ERR là một tuple của biểu mẫu được trả về bởi
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
47:
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
53.(test)

Việc triển khai mặc định nối thêm một Tuple

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
49 vào thuộc tính của thuộc tính ____ ____655, trong đó định dạng_err là một dấu vết được định dạng xuất phát từ ERR.

________ 656 (kiểm tra) ¶

Được gọi khi thử nghiệm thử nghiệm thành công.(test, reason)

Việc thực hiện mặc định không có gì.

________ 657 (kiểm tra, lý do) ¶

Được gọi khi thử nghiệm thử nghiệm bị bỏ qua. Lý do là lý do bài kiểm tra đưa ra để bỏ qua.(test, err)

Việc triển khai mặc định nối thêm một Tuple

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
58 vào thuộc tính của thuộc tính ____ ____659.

________ 660 (Kiểm tra, ERR) ¶

Được gọi khi thử nghiệm thử nghiệm không thành công hoặc lỗi, nhưng được đánh dấu bằng trình trang trí
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
82.(test)

Việc triển khai mặc định nối thêm một Tuple

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
49 vào thuộc tính của thuộc tính ____ ____663, trong đó định dạng_err là một dấu vết được định dạng có nguồn gốc từ ERR.

________ 664 (kiểm tra) ¶

Được gọi là khi thử nghiệm thử nghiệm được đánh dấu bằng trình trang trí
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
82, nhưng đã thành công.(test, subtest, outcome)

Việc triển khai mặc định nối thêm thử nghiệm vào thuộc tính của thuộc tính

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
66.

________ 667 (kiểm tra, tinh tế nhất, kết quả) ¶

Được gọi là khi một kết thúc phụ nhất. Kiểm tra là trường hợp thử nghiệm tương ứng với phương pháp thử nghiệm. Subtest là một thể hiện tùy chỉnh ____55 mô tả bài kiểm tra nhất.

Nếu kết quả là

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33, phần phụ đã thành công. Mặt khác, nó đã thất bại với một ngoại lệ trong đó kết quả là một tuple của biểu mẫu được trả về bởi
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
47:
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
53.

Việc thực hiện mặc định không làm gì khi kết quả là một thành công và ghi lại các thất bại tinh tế là thất bại bình thường.(stream, descriptions, verbosity)

Mới trong phiên bản 3.4.

Lớp ________ 185 ________ 673 (Stream, Motions, Verbosity) ¶This class was previously named

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
76. The old name still exists as an alias but is deprecated.

Một triển khai cụ thể của
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76 được sử dụng bởi
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
39.

Mới trong phiên bản 3.2: Lớp này trước đây được đặt tên là

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
76. Tên cũ vẫn tồn tại dưới dạng bí danh nhưng không được dùng.

________ 185 ________ 678¶(stream=None, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, warnings=None, *, tb_locals=False)

Ví dụ của lớp

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
28 dự định được chia sẻ. Nếu không cần tùy chỉnh
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
28, trường hợp này có thể được sử dụng thay vì liên tục tạo các trường hợp mới.

Lớp ________ 185 ________ 682 (stream = none, mô tả = true, verbosity = 1, failfast = falseignored by default. Deprecation warnings caused by deprecated unittest methods are also special-cased and, when the warning filters are

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
90 or
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
91, they will appear only once per-module, in order to avoid too many warning messages. This behavior can be overridden using Python’s
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
92 or
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
93 options (see Warning control) and leaving warnings to
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33.

Một triển khai người chạy thử nghiệm cơ bản đưa ra kết quả cho một luồng. Nếu luồng là

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33, mặc định,
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
24 được sử dụng làm luồng đầu ra. Lớp này có một vài tham số có thể định cấu hình, nhưng về cơ bản rất đơn giản. Các ứng dụng đồ họa chạy các bộ thử nghiệm nên cung cấp các triển khai thay thế. Các triển khai như vậy sẽ chấp nhận
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
85 khi giao diện để xây dựng các thay đổi của người chạy khi các tính năng được thêm vào Unittest.Added the
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
95 argument.

Theo mặc định, người chạy này hiển thị

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
86,
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
87,
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
88 và
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
89 ngay cả khi chúng bị bỏ qua theo mặc định. Các cảnh báo từ chối gây ra bởi các phương pháp không thể bỏ qua cũng được giới thiệu đặc biệt và khi các bộ lọc cảnh báo là
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
90 hoặc
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
91, chúng sẽ chỉ xuất hiện một lần theo mô-đun, để tránh quá nhiều tin nhắn cảnh báo. Hành vi này có thể được ghi đè bằng các tùy chọn Python từ ____ ____692 hoặc
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
93 (xem kiểm soát cảnh báo) và để lại cảnh báo về
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33.The default stream is set to
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
24 at instantiation time rather than import time.

Đã thay đổi trong phiên bản 3.2: Đã thêm đối số

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
95.Added the tb_locals parameter.

Đã thay đổi trong phiên bản 3.2: Luồng mặc định được đặt thành
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
24 tại thời điểm khởi tạo thay vì nhập thời gian.()

Đã thay đổi trong phiên bản 3.5: Đã thêm tham số TB_LOCALS.

________ 697 ()

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
8

________ 231 (kiểm tra) ¶(test)

Phương pháp này là giao diện công cộng chính cho

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
39. Phương pháp này có một ví dụ
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59 hoặc
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5. Một
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76 được tạo bằng cách gọi
python -m unittest tests/test_something.py
01 và (các) bài kiểm tra được chạy và kết quả được in thành stdout.

________ 185 ________ 713 (mô -đun = '__ main__', defaultTest = none ¶(module='__main__', defaultTest=None, argv=None, testRunner=None, testLoader=unittest.defaultTestLoader, exit=True, verbosity=1, failfast=None, catchbreak=None, buffer=None, warnings=None)

Một chương trình dòng lệnh tải một tập hợp các thử nghiệm từ mô-đun và chạy chúng; Điều này chủ yếu để thực hiện các mô -đun thử nghiệm có thể thực thi thuận tiện. Việc sử dụng đơn giản nhất cho chức năng này là bao gồm dòng sau ở cuối tập lệnh kiểm tra:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()
9

Bạn có thể chạy các bài kiểm tra với thông tin chi tiết hơn bằng cách truyền trong đối số xác suất:

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
0

Đối số DefaultTest là tên của một bài kiểm tra hoặc một tên thử nghiệm có thể chạy nếu không có tên thử nghiệm nào được chỉ định qua Argv. Nếu không được chỉ định hoặc

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33 và không có tên thử nghiệm nào được cung cấp qua Argv, tất cả các thử nghiệm được tìm thấy trong mô -đun được chạy.

Đối số Argv có thể là danh sách các tùy chọn được truyền cho chương trình, với phần tử đầu tiên là tên chương trình. Nếu không được chỉ định hoặc

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33, các giá trị của
python -m unittest tests/test_something.py
16 được sử dụng.

Đối số testrunner có thể là một lớp người chạy thử hoặc một thể hiện đã được tạo của nó. Theo mặc định

python -m unittest tests/test_something.py
17 gọi
python -m unittest tests/test_something.py
18 với mã thoát cho biết thành công hoặc thất bại của các bài kiểm tra chạy.

Đối số TestLoader phải là một ví dụ

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
28 và mặc định là
python -m unittest tests/test_something.py
20.

python -m unittest tests/test_something.py
17 hỗ trợ được sử dụng từ trình thông dịch tương tác bằng cách truyền trong đối số
python -m unittest tests/test_something.py
22. Điều này hiển thị kết quả trên đầu ra tiêu chuẩn mà không cần gọi
python -m unittest tests/test_something.py
18:

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
1

Các tham số Failfast, Catchbreak và bộ đệm có hiệu ứng tương tự như các tùy chọn dòng lệnh cùng tên.

Đối số cảnh báo chỉ định bộ lọc cảnh báo nên được sử dụng trong khi chạy các bài kiểm tra. Nếu nó không được chỉ định, nó sẽ vẫn còn ____233 nếu tùy chọn

python -m unittest tests/test_something.py
25 được chuyển sang Python (xem kiểm soát cảnh báo), nếu không nó sẽ được đặt thành
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
90.warning filter that should be used while running the tests. If it’s not specified, it will remain
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33 if a
python -m unittest tests/test_something.py
25 option is passed to python (see Warning control), otherwise it will be set to
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
90.

Gọi

python -m unittest tests/test_something.py
17 thực sự trả về một thể hiện của lớp
python -m unittest tests/test_something.py
28. Điều này lưu trữ kết quả của các thử nghiệm chạy dưới dạng thuộc tính
python -m unittest tests/test_something.py
29.

Thay đổi trong phiên bản 3.1: Tham số thoát đã được thêm vào.The exit parameter was added.

Đã thay đổi trong phiên bản 3.2: Các thông số về độ verbosity, failfast, catchbreak, bộ đệm và cảnh báo đã được thêm vào.The verbosity, failfast, catchbreak, buffer and warnings parameters were added.

Đã thay đổi trong phiên bản 3.4: Tham số DefaultTest đã được thay đổi để cũng chấp nhận một tên thử nghiệm.The defaultTest parameter was changed to also accept an iterable of test names.

Giao thức Load_Tests

Mới trong phiên bản 3.2.

Các mô -đun hoặc gói có thể tùy chỉnh cách các thử nghiệm được tải từ chúng trong quá trình chạy thử nghiệm thông thường hoặc khám phá thử nghiệm bằng cách thực hiện một hàm gọi là

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44.

Nếu một mô -đun thử nghiệm xác định

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44, nó sẽ được gọi bởi
python -m unittest tests/test_something.py
32 với các đối số sau:

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
2

trong đó mẫu được truyền thẳng từ

python -m unittest tests/test_something.py
33. Nó mặc định là
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
33.

Nó sẽ trả lại một

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59.

Trình tải là ví dụ của

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
28 thực hiện tải. Standard_Tests là các thử nghiệm sẽ được tải theo mặc định từ mô -đun. Người ta thường chỉ muốn thêm hoặc xóa các bài kiểm tra khỏi bộ kiểm tra tiêu chuẩn. Đối số thứ ba được sử dụng khi tải các gói như một phần của khám phá thử nghiệm.

Một hàm

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44 điển hình tải các bài kiểm tra từ một tập hợp các lớp
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 cụ thể có thể trông giống như:

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
3

Nếu Discovery được bắt đầu trong một thư mục chứa gói, từ dòng lệnh hoặc bằng cách gọi

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
05, thì gói
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
69 sẽ được kiểm tra
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44. Nếu chức năng đó không tồn tại, Discovery sẽ tái phát vào gói như thể nó chỉ là một thư mục khác. Mặt khác, việc phát hiện ra các thử nghiệm của gói sẽ được để lại lên đến
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44 được gọi với các đối số sau:

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
2

Điều này sẽ trả về

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59 đại diện cho tất cả các thử nghiệm từ gói. (
python -m unittest tests/test_something.py
44 sẽ chỉ chứa các thử nghiệm được thu thập từ
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
69.)

Bởi vì mẫu được chuyển vào

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44, gói miễn phí để tiếp tục (và có khả năng sửa đổi) khám phá thử nghiệm. A ‘Không làm gì chức năng
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
44 cho gói thử nghiệm sẽ giống như:

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
5

Đã thay đổi trong phiên bản 3.5: Discovery không còn kiểm tra tên gói cho mẫu phù hợp do không thể có tên gói phù hợp với mẫu mặc định.Discovery no longer checks package names for matching pattern due to the impossibility of package names matching the default pattern.

Lớp và mô -đun đồ đạc

Đồ đạc cấp độ và mô -đun được thực hiện trong

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
59. Khi bộ kiểm tra gặp phải một bài kiểm tra từ một lớp mới thì
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
02 từ lớp trước (nếu có) được gọi, theo sau là
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
01 từ lớp mới.

Tương tự nếu một thử nghiệm là từ một mô -đun khác với thử nghiệm trước đó

python -m unittest tests/test_something.py
51 từ mô -đun trước được chạy, theo sau là
python -m unittest tests/test_something.py
52 từ mô -đun mới.

Sau khi tất cả các bài kiểm tra đã chạy cuối cùng

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
29 và
python -m unittest tests/test_something.py
51 được chạy.

Lưu ý rằng đồ đạc được chia sẻ không chơi tốt với các tính năng [tiềm năng] như song song thử nghiệm và chúng phá vỡ sự cô lập thử nghiệm. Chúng nên được sử dụng một cách chăm sóc.

Thứ tự mặc định của các thử nghiệm được tạo bởi các trình tải thử nghiệm không nhất quán là nhóm tất cả các thử nghiệm từ cùng một mô -đun và các lớp với nhau. Điều này sẽ dẫn đến

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
26 /
python -m unittest tests/test_something.py
52 (v.v.) được gọi chính xác một lần mỗi lớp và mô -đun. Nếu bạn chọn ngẫu nhiên thứ tự, để các thử nghiệm từ các mô -đun và lớp khác nhau liền kề với nhau, thì các chức năng cố định được chia sẻ này có thể được gọi nhiều lần trong một lần chạy thử.

Đồ đạc được chia sẻ không nhằm mục đích làm việc với các bộ với đặt hàng không chuẩn. Một

python -m unittest tests/test_something.py
57 vẫn tồn tại cho các khung không muốn hỗ trợ đồ đạc được chia sẻ.

Nếu có bất kỳ trường hợp ngoại lệ nào được nêu trong một trong các chức năng cố định được chia sẻ, thử nghiệm được báo cáo là một lỗi. Do không có phiên bản kiểm tra tương ứng, một đối tượng

python -m unittest tests/test_something.py
58 (có giao diện tương tự như
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5) được tạo để biểu thị lỗi. Nếu bạn chỉ sử dụng trình chạy thử nghiệm Unittest tiêu chuẩn thì chi tiết này không quan trọng, nhưng nếu bạn là tác giả khung thì có thể có liên quan.

Lớp học và Lớp học Teakeddross¶

Chúng phải được thực hiện như các phương thức lớp:

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
6

Nếu bạn muốn

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
26 và
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
29 trên các lớp cơ sở được gọi thì bạn phải tự gọi cho họ. Việc triển khai trong
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
5 trống.

Nếu một ngoại lệ được nâng lên trong một

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
26 thì các bài kiểm tra trong lớp không được chạy và
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
29 không được chạy. Các lớp bị bỏ qua sẽ không có
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
26 hoặc
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
29 chạy. Nếu ngoại lệ là ngoại lệ
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
80 thì lớp sẽ được báo cáo là đã bị bỏ qua thay vì là một lỗi.

Thiết lập và TeakedDownOdule¶

Chúng nên được thực hiện dưới dạng chức năng:

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
7

Nếu một ngoại lệ được nêu trong

python -m unittest tests/test_something.py
52 thì không có thử nghiệm nào trong mô -đun sẽ được chạy và
python -m unittest tests/test_something.py
51 sẽ không được chạy. Nếu ngoại lệ là ngoại lệ
======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
80 thì mô -đun sẽ được báo cáo là đã bị bỏ qua thay vì là một lỗi.

Để thêm mã dọn dẹp phải chạy ngay cả trong trường hợp ngoại lệ, hãy sử dụng

python -m unittest tests/test_something.py
71:

________ 185 ________ 773 (chức năng, /, *args, ** kwargs) ¶(function, /, *args, **kwargs)

Thêm một chức năng để được gọi theo sau

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
04 vào các tài nguyên dọn dẹp được sử dụng trong lớp kiểm tra. Các chức năng sẽ được gọi theo thứ tự ngược lại theo thứ tự chúng được thêm vào (LIFO). Chúng được gọi với bất kỳ đối số và từ khóa nào được truyền vào
python -m unittest tests/test_something.py
75 khi chúng được thêm vào.

Nếu

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
03 thất bại, nghĩa là
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
04 không được gọi, thì bất kỳ chức năng dọn dẹp nào được thêm vào vẫn sẽ được gọi.

Mới trong phiên bản 3.8.

________ 185 ________ 779 ()()

Hàm này được gọi là vô điều kiện sau

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
04 hoặc sau
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
03 nếu
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
03 sẽ tăng một ngoại lệ.

Nó chịu trách nhiệm gọi tất cả các chức năng dọn dẹp được thêm vào bởi

python -m unittest tests/test_something.py
75. Nếu bạn cần các chức năng dọn dẹp sẽ được gọi trước
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
04 thì bạn có thể tự gọi
python -m unittest tests/test_something.py
85.

python -m unittest tests/test_something.py
85 Phương pháp bật ra khỏi ngăn xếp các chức năng dọn dẹp mỗi lần, vì vậy nó có thể được gọi bất cứ lúc nào.

Mới trong phiên bản 3.8.

________ 185 ________ 779 ()

Hàm này được gọi là vô điều kiện sau

======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
04 hoặc sau
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
03 nếu
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
03 sẽ tăng một ngoại lệ.

Nó chịu trách nhiệm gọi tất cả các chức năng dọn dẹp được thêm vào bởi

python -m unittest tests/test_something.py
75. Nếu bạn cần các chức năng dọn dẹp sẽ được gọi trước
======================================================================
FAIL: test_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 13, in test_useful
    self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'b': 2, 'c': [1, 2]} != {'a': 1, 'b': 2, 'c': [1]}
- {'a': 1, 'b': 2, 'c': [1, 2]}
?                         ---

+ {'a': 1, 'b': 2, 'c': [1]} 
04 thì bạn có thể tự gọi
python -m unittest tests/test_something.py
85.

python -m unittest tests/test_something.py
85 Phương pháp bật ra khỏi ngăn xếp các chức năng dọn dẹp mỗi lần, vì vậy nó có thể được gọi bất cứ lúc nào.

Xử lý tín hiệu

Mới trong phiên bản 3.2.()

Tùy chọn dòng lệnh

python -m unittest tests/test_something.py
87 để Unittest, cùng với tham số
python -m unittest tests/test_something.py
88 thành
python -m unittest tests/test_something.py
0, cung cấp cách xử lý thân thiện hơn của Control-C trong quá trình chạy thử. Với Catch Break, hành vi được kích hoạt Control-C sẽ cho phép thử nghiệm hiện đang chạy và chạy thử sau đó sẽ kết thúc và báo cáo tất cả các kết quả cho đến nay. Một điều khiển thứ hai sẽ tăng
python -m unittest -v test_module
4 theo cách thông thường.

Trình xử lý tín hiệu xử lý điều khiển-C cố gắng duy trì tương thích với mã hoặc kiểm tra cài đặt trình xử lý
python -m unittest tests/test_something.py
91 của riêng họ. Nếu trình xử lý
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2 được gọi nhưng không phải là trình xử lý
python -m unittest tests/test_something.py
91 được cài đặt, tức là nó đã được thay thế bởi hệ thống được thử nghiệm và được ủy quyền, thì nó gọi trình xử lý mặc định. Đây thường sẽ là hành vi dự kiến ​​bằng mã thay thế một trình xử lý đã cài đặt và ủy quyền cho nó. Đối với các bài kiểm tra riêng lẻ cần sử dụng Trình trang trí
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
2 Control-C, bộ trang trí
python -m unittest tests/test_something.py
95 có thể được sử dụng.(result)

Có một vài chức năng tiện ích cho các tác giả khung để cho phép chức năng xử lý Control-C trong các khung kiểm tra.

Đăng ký một đối tượng

======================================================================
FAIL: test_not_so_useful (__main__.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/file.py", line 10, in test_not_so_useful
    self.assertTrue(self.D1 == self.D2)
AssertionError: False is not true
76 không có tác dụng phụ nếu xử lý Control-C không được bật, vì vậy các khung kiểm tra có thể đăng ký vô điều kiện tất cả các kết quả mà chúng tạo ra độc lập về việc xử lý có được bật hay không.

________ 185 ________ 805 (kết quả) ¶(result)

Loại bỏ một kết quả đã đăng ký.Khi kết quả đã được xóa thì

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
20 sẽ không còn được gọi trên đối tượng kết quả đó để đáp ứng với Control-C.

________ 185 ________ 808 (hàm = không) ¶(function=None)

Khi được gọi mà không có đối số, chức năng này sẽ loại bỏ trình xử lý điều khiển-C nếu nó đã được cài đặt.Chức năng này cũng có thể được sử dụng như một công cụ trang trí thử nghiệm để tạm thời loại bỏ trình xử lý trong khi thử nghiệm đang được thực thi:

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
8