Hướng dẫn assert not implemented python - khẳng định chưa triển khai python

Ban đầu tôi đã không sử dụng khung

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

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

OK
9, vì vậy để kiểm tra rằng hai đối tượng của cùng một lớp không thể so sánh bằng cách sử dụng các toán tử
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
0 và
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
1 Tôi đã làm một cái gì đó như:

try:
    o1 < o2
    assert False
except TypeError:
    pass

Tuy nhiên, sau đó, tôi quyết định bắt đầu sử dụng mô -đun

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

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

OK
9, vì vậy tôi đang chuyển đổi các bài kiểm tra của mình sang cách các bài kiểm tra được viết với cùng một mô -đun.

Tôi đã cố gắng hoàn thành điều tương đương như trên với:

self.assertRaises(TypeError, o1 < o2)

Nhưng điều này không hoàn toàn hoạt động, bởi vì

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
3 cố gắng gọi người vận hành
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
0, thay vì là một tham chiếu đến một hàm, nên được gọi là một phần của thử nghiệm.

Có cách nào để thực hiện những gì tôi cần mà không cần phải bọc

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
3 trong một chức năng không?

Mã nguồn: lib/unittest/__ init__.py 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.)the list of assert methods.)

Khung thử nghiệm đơn vị

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 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,

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 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ể.

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 cung cấp một lớp cơ sở,
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9, có thể được sử dụng để tạo các trường hợp thử nghiệm mới.

Bộ kiểm tra

Một bộ thử nghiệm là một tập hợp các trường hợp thử nghiệm, bộ thử nghiệm hoặc cả hai. Nó được sử dụng để tổng hợp các bài kiểm tra nên được thực hiện cùng nhau.

Người chạy thử

Người chạy thử là một thành phần phối hợp thực hiện các bài kiểm tra và cung cấp kết quả cho người dùng. Người chạy có thể sử dụng giao diện đồ họa, giao diện văn bản hoặc trả về giá trị đặc biệt để chỉ ra kết quả thực hiện các thử nghiệm.

Xem thêm

Mô -đun
python -m unittest tests/test_something.py
0

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

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

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ụ,

python -m unittest tests/test_something.py
2.

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

python -m unittest tests/test_something.py
3 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, GitHub Action hoặc AppVeyor.

Ví dụ cơ bản

Mô -đun

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 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 tests/test_something.py
5. 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 tests/test_something.py
6. 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 tests/test_something.py
7 để kiểm tra kết quả dự kiến;
python -m unittest tests/test_something.py
8 hoặc
python -m unittest tests/test_something.py
9 để xác minh một điều kiện; hoặc
python -m unittest -v test_module
0 để 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 -v test_module
1 để 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 -v test_module
2 và
python -m unittest -v test_module
3 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 -v test_module
4 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 -v test_module
5 cho tập lệnh kiểm tra của bạn sẽ hướng dẫn
python -m unittest -v test_module
4 để cho phép mức độ xác thực cao hơn và tạo ra đầu ra sau:

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

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

OK

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

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 đượ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.

Đã thay đổi trong phiên bản 3.11: Hành vi trả về giá trị từ phương thức kiểm tra (trừ giá trị

python -m unittest -v test_module
8 mặc định), hiện không được dùng nữa.The behavior of returning a value from a test method (other than the default
python -m unittest -v test_module
8 value), is now deprecated.

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:

-B,-Buffer¶, --buffer

Đầ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.

-c,-bắt Năm,--catch

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
9 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.

-f,-failfast¶, --failfast

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

-K¶

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
0) được khớp với tên thử nghiệm bằng cách sử dụng
cd project_directory
python -m unittest discover
1; 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
2 khớp với
cd project_directory
python -m unittest discover
3,
cd project_directory
python -m unittest discover
4, nhưng không phải
cd project_directory
python -m unittest discover
5.

-Locals¶

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

cd project_directory
python -m unittest discover
6,
cd project_directory
python -m unittest discover
7 và
cd project_directory
python -m unittest discover
8 đã được thêm vào.The command-line options
cd project_directory
python -m unittest discover
6,
cd project_directory
python -m unittest discover
7 and
cd project_directory
python -m unittest discover
8 were added.

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

cd project_directory
python -m unittest discover
9.The command-line option
cd project_directory
python -m unittest discover
9.

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

python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
0.The command-line option
python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
0.

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

python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
1, 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,

python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
2 là tương đương với
python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
3. Nếu bạn muốn vượt qua các đối số để kiểm tra khám phá, chỉ huy phụ
python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
4 phải được sử dụng rõ ràng.

Trình lệnh phụ

python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
4 có các tùy chọn sau:

-V,-Verbose¶,--verbose

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

-S,-DirectoryDirectoryr của Start-DirectoryDirectory,--start-directory directory

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

python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
6)

-P,-statesTpotyn¶,--pattern pattern

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

python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
7)

-T,-cấp độ hàng đầu-định hướng,--top-level-directory directory

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

python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
8,
python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
9 và
self.assertRaises(TypeError, o1 < o2)
00 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:

python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"

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

self.assertRaises(TypeError, o1 < o2)
01, 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ụ

self.assertRaises(TypeError, o1 < o2)
02 sẽ được nhập dưới dạng
self.assertRaises(TypeError, o1 < o2)
03.

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

self.assertRaises(TypeError, o1 < o2)
04).Test discovery supports namespace packages for the start directory. Note that you need to specify the top level directory too (e.g.
self.assertRaises(TypeError, o1 < o2)
04).

Đã thay đổi trong phiên bản 3.11: Python 3.11 đã bỏ các gói không gian tên hỗ trợ. Nó đã bị phá vỡ kể từ Python 3.7. Bắt đầu thư mục và thư mục con chứa các thử nghiệm phải là gói thông thường có tệp ____105.Python 3.11 dropped the namespace packages support. It has been broken since Python 3.7. Start directory and subdirectories containing tests must be regular package that have

self.assertRaises(TypeError, o1 < o2)
05 file.

Các thư mục có chứa thư mục bắt đầu vẫn có thể là một gói không gian tên. Trong trường hợp này, bạn cần chỉ định thư mục bắt đầu dưới dạng tên gói chấm và thư mục đích một cách rõ ràng. Ví dụ:

self.assertRaises(TypeError, o1 < o2)
0

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

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6, các trường hợp thử nghiệm được biểu thị bằng các trường hợp
python -m unittest tests/test_something.py
5. Để 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à
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 hoặc sử dụng
self.assertRaises(TypeError, o1 < o2)
09.

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

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 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

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 đơ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 tests/test_something.py
6) để thực hiện mã kiểm tra cụ thể:

self.assertRaises(TypeError, o1 < o2)
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

self.assertRaises(TypeError, o1 < o2)
13 được cung cấp bởi lớp cơ sở
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9. 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à
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 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 -v test_module
2, mà khung thử nghiệm sẽ tự động gọi cho mỗi lần thử nghiệm mà chúng tôi chạy:

self.assertRaises(TypeError, o1 < o2)
2

Ghi chú

Thứ tự mà các thử nghiệm khác nhau sẽ được chạy được xác định bằng cách sắp xếp các tên phương thức kiểm tra liên quan đến thứ tự tích hợp cho các chuỗi.

Nếu phương thức

python -m unittest -v test_module
2 làm tăng một ngoại lệ trong khi thử nghiệm đang chạy, khung sẽ xem xét thử nghiệm đã bị lỗi và phương pháp kiểm tra sẽ không được thực thi.

Tương tự, chúng tôi có thể cung cấp một phương thức

python -m unittest -v test_module
3 phù hợp sau khi phương thức kiểm tra đã được chạy:

self.assertRaises(TypeError, o1 < o2)
3

Nếu

python -m unittest -v test_module
2 thành công,
python -m unittest -v test_module
3 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 -v test_module
2,
python -m unittest -v test_module
3 và
self.assertRaises(TypeError, o1 < o2)
23 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.

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 cung cấp một cơ chế cho điều này: Bộ thử nghiệm, được đại diện bởi lớp ____ 56 ____ ____126. Trong hầu hết các trường hợp, gọi
python -m unittest -v test_module
4 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 đó:

self.assertRaises(TypeError, o1 < o2)
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ư

self.assertRaises(TypeError, o1 < o2)
28), 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ư
self.assertRaises(TypeError, o1 < o2)
29:

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

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6, mà không chuyển đổi mọi chức năng kiểm tra cũ thành lớp con
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9.

Vì lý do này,

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 cung cấp một lớp
self.assertRaises(TypeError, o1 < o2)
09. Lớp con này của
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 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:

self.assertRaises(TypeError, o1 < o2)
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:

self.assertRaises(TypeError, o1 < o2)
6

Ghi chú

Mặc dù

self.assertRaises(TypeError, o1 < o2)
09 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 ________ 56, 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
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 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

python -m unittest tests/test_something.py
0. Nếu vậy,
python -m unittest tests/test_something.py
0 cung cấp một lớp
self.assertRaises(TypeError, o1 < o2)
40 có thể tự động xây dựng các trường hợp
self.assertRaises(TypeError, o1 < o2)
41 từ các thử nghiệm dựa trên ________ 60 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

self.assertRaises(TypeError, o1 < o2)
43.

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

self.assertRaises(TypeError, o1 < o2)
44 hoặc một trong những biến thể có điều kiện của nó, gọi
self.assertRaises(TypeError, o1 < o2)
45 trong phương pháp
python -m unittest -v test_module
2 hoặc thử nghiệm hoặc tăng trực tiếp
self.assertRaises(TypeError, o1 < o2)
47.decorator or one of its conditional variants, calling
self.assertRaises(TypeError, o1 < o2)
45 within a
python -m unittest -v test_module
2 or test method, or raising
self.assertRaises(TypeError, o1 < o2)
47 directly.

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

self.assertRaises(TypeError, o1 < o2)
7

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

self.assertRaises(TypeError, o1 < o2)
8

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

self.assertRaises(TypeError, o1 < o2)
9

self.assertRaises(TypeError, o1 < o2)
48 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 nhà trang trí

self.assertRaises(TypeError, o1 < o2)
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()
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

self.assertRaises(TypeError, o1 < o2)
44 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:

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

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:

@unittest.skip (lý do) ¶unittest.skip(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.

@unittest.skipif (điều kiện, lý do) ¶unittest.skipIf(condition, reason)

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

@unittest.skipunless (điều kiện, lý do) ¶unittest.skipUnless(condition, reason)

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

@unittest.expectedfailure¶unittest.expectedFailure

Đá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.

ExceptionUnItest.skiptest (lý do) ¶unittest.SkipTest(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

self.assertRaises(TypeError, o1 < o2)
45 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 -v test_module
2 hoặc
python -m unittest -v test_module
3 chạy xung quanh chúng. Các lớp bị bỏ qua sẽ không có
self.assertRaises(TypeError, o1 < o2)
54 hoặc
self.assertRaises(TypeError, o1 < o2)
55 chạy. Các mô -đun bỏ qua sẽ không có
self.assertRaises(TypeError, o1 < o2)
56 hoặc
self.assertRaises(TypeError, o1 < o2)
57 chạy.

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.

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

self.assertRaises(TypeError, o1 < o2)
58.

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

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

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

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

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

self.assertRaises(TypeError, o1 < o2)
59 sẽ được hiển thị:

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

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

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

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

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

classunittest.testcase (methodname = 'runtest') ¶ unittest.TestCase(methodName='runTest')

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

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 đại diện cho các đơn vị kiểm tra logic trong vũ trụ
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6. 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

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 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
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9, bạn sẽ không thay đổi tên phương thức cũng như tái tạo phương thức
self.assertRaises(TypeError, o1 < o2)
65 mặc định.

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

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 có thể được khởi tạo thành công mà không cung cấp tên phương thức. Điều này giúp dễ dàng thử nghiệm với
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 từ trình thông dịch tương tác.
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 can be instantiated successfully without providing a methodName. This makes it easier to experiment with
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 from the interactive interpreter.

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 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à:

thành lập()¶()

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

self.assertRaises(TypeError, o1 < o2)
69 hoặc
self.assertRaises(TypeError, o1 < o2)
47, 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ì.

phá bỏ()¶()

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

self.assertRaises(TypeError, o1 < o2)
69 hoặc
self.assertRaises(TypeError, o1 < o2)
47, đượ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 -v test_module
2 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ì.

Lớp thiết lập () ¶()

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.

self.assertRaises(TypeError, o1 < o2)
74 được gọi với lớp là đối số duy nhất và phải được trang trí dưới dạng
self.assertRaises(TypeError, o1 < o2)
75:

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

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

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

Lớp học rehown () ¶()

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.

self.assertRaises(TypeError, o1 < o2)
76 được gọi với lớp là đối số duy nhất và phải được trang trí dưới dạng
self.assertRaises(TypeError, o1 < o2)
75:

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

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

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

Lớp học rehown () ¶(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.

self.assertRaises(TypeError, o1 < o2)
76 được gọi với lớp là đối số duy nhất và phải được trang trí dưới dạng
self.assertRaises(TypeError, o1 < o2)
75:

chạy (result = none) ¶

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

self.assertRaises(TypeError, o1 < o2)
43 được truyền qua kết quả. Nếu kết quả bị bỏ qua hoặc
python -m unittest -v test_module
8, 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
self.assertRaises(TypeError, o1 < o2)
80) và được sử dụng. Đối tượng kết quả được trả lại cho người gọi ____ 181.Previous versions of
self.assertRaises(TypeError, o1 < o2)
83 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
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9.
(reason)

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

self.assertRaises(TypeError, o1 < o2)
83 không trả về kết quả. Không gọi một ví dụ.Skipping tests and expected failures for more information.

Skiptest (lý do) ¶

Gọi điều này trong một phương pháp kiểm tra hoặc
python -m unittest -v test_module
2 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.

Subtest (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ộ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.()

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

gỡ lỗi ()

Chạy bài kiểm tra 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.

Lớp

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 cung cấp một số phương pháp khẳng định để kiểm tra và báo cáo thất bại. Bảng sau liệt kê các phương thức được sử dụng phổ biến nhất (xem các bảng bên dưới để biết thêm các phương thức khẳng định):

Phương pháp

self.assertRaises(TypeError, o1 < o2)
86

self.assertRaises(TypeError, o1 < o2)
87

self.assertRaises(TypeError, o1 < o2)
88

self.assertRaises(TypeError, o1 < o2)
89

self.assertRaises(TypeError, o1 < o2)
90

self.assertRaises(TypeError, o1 < o2)
91

self.assertRaises(TypeError, o1 < o2)
92

self.assertRaises(TypeError, o1 < o2)
93

self.assertRaises(TypeError, o1 < o2)
94

self.assertRaises(TypeError, o1 < o2)
95

3.1

self.assertRaises(TypeError, o1 < o2)
96

self.assertRaises(TypeError, o1 < o2)
97

3.1

self.assertRaises(TypeError, o1 < o2)
98

self.assertRaises(TypeError, o1 < o2)
99

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()
00

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()
01

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()
02

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

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()
04

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()
05

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()
06

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()
07

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()
08

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()
09

3.2

Kiểm tra điều đó

assertequal (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

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()
15 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:

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 được thêm vào dưới dạng hàm bình đẳng loại mặc định để so sánh cá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()
16 added as the default type equality function for comparing strings.

assertNotequal (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.

assertTrue (expr, msg = none) ¶ assertFalse (expr, msg = none) ¶(expr, msg=None)assertFalse(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

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 và khô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()
18 (sử dụ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()
19 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ụ:
self.assertRaises(TypeError, o1 < o2)
86 thay 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()
21), 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.

Assertis (thứ nhất, thứ hai, msg = none) ¶ AssertisNot (thứ nhất, thứ hai, msg = none)(first, second, msg=None)assertIsNot(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.

assertisNone (expr, msg = none) ¶ assertisnotnone (expr, msg = none) ¶(expr, msg=None)assertIsNotNone(expr, msg=None)

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

python -m unittest -v test_module
8.

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

assertisNone (expr, msg = none) ¶ assertisnotnone (expr, msg = none) ¶(member, container, msg=None)assertNotIn(member, container, msg=None)

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

python -m unittest -v test_module
8.

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

assertisNone (expr, msg = none) ¶ assertisnotnone (expr, msg = none) ¶(obj, cls, msg=None)assertNotIsInstance(obj, cls, msg=None)

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

python -m unittest -v test_module
8.

Assertin (Thành viên, Container, MSG = Không) ¶ AssertNotin (Thành viên, Container, MSG = Không) ¶

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

assertisInstance (obj, cls, msg = none) ¶ assertNotisInstance (obj, cls, msg = none)

Kiểm tra rằng OBJ là (hoặc không) là một thể hiện của CLS (có thể là một lớp hoặc một nhóm các lớp, được hỗ trợ 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()
23). Để kiểm tra loại chính xác, sử dụ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()
24.

Mới trong phiên bản 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()
25

Cũng có thể kiểm tra việc sản xuất các ngoại lệ, cảnh báo và tin nhắn nhật ký bằng các phương pháp sau:

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()
27

Phương pháp

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()
29

Kiểm tra điều đó

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()
31

Mớ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()
33

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()
26 tăng exc

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()
35

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()
26 tăng exc và tin nhắn phù hợp với regex r

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()
26 tăng cảnh báo

3.10

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()
26 tăng cảnh báo và tin nhắn phù hợp với regex r
(exception, callable, *args, **kwds)assertRaises(exception, *, msg=None)

Nhật ký 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()
34 trên logger với mức tối thiểu

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()
34 không đăng nhậ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()
7

logger với mức tối thiểu

AssertRaises (ngoại lệ, có thể gọi, *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()
8

Kiểm tra rằng một ngoại lệ được nêu ra 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

python -m unittest -v test_module
0. Bài kiểm tra vượt qua nếu ngoại lệ được nâng lên, là một lỗi nếu một ngoại lệ khác được nêu ra hoặc không thành công nếu không có ngoại lệ nào được nâng lên. Để bắt bất kỳ nhóm ngoại lệ nào, một tuple chứa các lớp ngoại lệ có thể được truyền dưới dạng ngoại lệ.Added the ability to use
python -m unittest -v test_module
0 as a context manager.

Nếu chỉ có ngoại lệ và có thể được đư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 thay vì là một hàm: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()
39 attribute.

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

python -m unittest -v test_module
0 chấp nhận thông tin từ khóa bổ sung.Added the msg keyword argument when used as a context manager.

Trình quản lý bối cảnh sẽ lưu trữ đối tượng ngoại lệ bị bắt trong 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()
39 của nó. Điều này có thể hữu ích nếu ý định là thực hiện kiểm tra bổ sung về ngoại lệ được nêu ra:
(exception, regex, callable, *args, **kwds)assertRaisesRegex(exception, regex, *, msg=None)

Đã thay đổi trong phiên bản 3.1: Đã thêm khả năng sử dụng

python -m unittest -v test_module
0 làm trình quản lý ngữ cả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()
9

or:

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

OK
0

Đã thay đổi trong phiên bản 3.2: Đã thêm 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()
39.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()
44.

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

python -m unittest -v test_module
0 chấp nhận thông tin từ khóa bổ sung.Added the msg keyword argument when used as a context manager.

Trình quản lý bối cảnh sẽ lưu trữ đối tượng ngoại lệ bị bắt trong 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()
39 của nó. Điều này có thể hữu ích nếu ý định là thực hiện kiểm tra bổ sung về ngoại lệ được nêu ra:
(warning, callable, *args, **kwds)assertWarns(warning, *, msg=None)

Đã thay đổi trong phiên bản 3.1: Đã thêm khả năng sử dụng

python -m unittest -v test_module
0 làm trình quản lý ngữ cảnh.

Đã thay đổi trong phiên bản 3.2: Đã thêm 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()
39.

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

OK
1

Khi được sử dụng làm trình quản lý ngữ cả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()
13 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

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 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()
48 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()
49. Đ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:

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

OK
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.

AssertWarnsRegex (Cảnh báo, Regex, Callable, *Args, ** KWDS)(warning, regex, callable, *args, **kwds)assertWarnsRegex(warning, regex, *, msg=None)

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()
13 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()
43. Thí dụ:

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

OK
3

or:

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

OK
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.

AssertWarnsRegex (Cảnh báo, Regex, Callable, *Args, ** KWDS)(logger=None, level=None)

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()
13 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()
43. Thí dụ:

AssertLogs (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()
52 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()
53 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()
54 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()
55). 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()
56.

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()
34 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:

Hồ sơ¶

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()
58 của các thông báo nhật ký phù hợp.

Example:

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

OK
5

đầu ra

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()
53 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.

AssertNologs (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()
52 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()
53 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()
54 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()
55). 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()
56.

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()
34 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:

Hồ sơ¶

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()
58 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()
66

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()
67

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

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

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()
70

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

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()
72

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

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()
74

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

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()
76

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

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()
78

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

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()
80

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

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()
82

đầu ra

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()
53 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)assertNotAlmostEqual(first, second, places=7, msg=None, delta=None)

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

AssertNologs (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()
52 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()
53 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()
85 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()
86 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()
65, sẽ không có gì được trả về bởi người quản lý bối cảnh.
(first, second, msg=None)assertGreaterEqual(first, second, msg=None)assertLess(first, second, msg=None)assertLessEqual(first, second, msg=None)

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

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

OK
6

Ngoài ra còn có các phương pháp khác được sử dụng để thực hiện kiểm tra cụ thể hơn, chẳng hạn như:

Phương pháp(text, regex, msg=None)assertNotRegex(text, regex, msg=None)

Kiểm tra điều đó

MớiAdded 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()
88.

A và B có cùng các yếu tố trong cùng một số, bất kể thứ tự của chúng.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()
89 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()
90.

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()
91 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()
92.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()
91 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()
92.

assertCountequal (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()
93 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 tests/test_something.py
7 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
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()
15:

AddTyPeequalityFunc (typeObj, function) ¶(typeobj, function)

Đăng ký một phương thức cụ thể loại được gọi bởi

python -m unittest tests/test_something.py
7 để 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 tests/test_something.py
7. 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()
98 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 tests/test_something.py
7 đượ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

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

OK
00

dây

3.1

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

OK
01

trình tự

3.1

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

OK
02

danh sách

3.1

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

OK
03

bộ dữ liệu

3.1

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

OK
04

bộ hoặc đông lạnh

3.1

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

OK
05

chỉ đạo

3.1

AssertMultilineEqual (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 tests/test_something.py
7.

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 tests/test_something.py
7 đượ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.
(first, second, msg=None, seq_type=None)

Phương pháp

Được sử dụng để so sánh

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 tests/test_something.py
7 đượ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.
(first, second, msg=None)assertTupleEqual(first, second, msg=None)

Phương pháp

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 tests/test_something.py
7 đượ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.
(first, second, msg=None)

Phương pháp

Được sử dụng để so sánh

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 tests/test_something.py
7 đượ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.
(first, second, msg=None)

Phương pháp

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 tests/test_something.py
7 đượ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(msg=None)

Được sử dụng để so sánh

Mới

dây

trình tự

danh sách

bộ dữ liệu

bộ hoặc đông lạnh

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 tests/test_something.py
7 đượ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ài đặt

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

OK
24 thành
python -m unittest -v test_module
8 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:

CounttestCase ()()

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

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9, điều này sẽ luôn là
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
27.

DefaultTestresult ()()

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

self.assertRaises(TypeError, o1 < o2)
81).

Đối với 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
9, đây sẽ luôn là một ví dụ của
self.assertRaises(TypeError, o1 < o2)
43; Các lớp con của
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 nên ghi đè lên điều này khi cần thiết.

Tôi()¶()

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.

Mô tả ngắn()¶()

Trả về mô tả của bài kiểm tra, hoặc

python -m unittest -v test_module
8 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 DocString, nếu có hoặc
python -m unittest -v test_module
8.

Đã 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
34 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
34 in Python 3.2.

addcleanup (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 -v test_module
3 để 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
36 khi chúng được thêm vào.

Nếu

python -m unittest -v test_module
2 thất bại, có nghĩa là
python -m unittest -v test_module
3 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.

Entercontext (cm) ¶(cm)

Nhập Trình quản lý bối cảnh được cung cấp. Nếu thành công, cũng thêm phương thức

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

OK
39 làm hàm làm sạch bằng
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
36 và trả về kết quả của phương thức
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
41.context manager. If successful, also add its
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
39 method as a cleanup function by
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
36 and return the result of the
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
41 method.

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

docleanups ()()

Phương pháp này được gọi là vô điều kiện sau

python -m unittest -v test_module
3 hoặc sau
python -m unittest -v test_module
2 nếu
python -m unittest -v test_module
2 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

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

OK
36. Nếu bạn cần các chức năng dọn dẹp sẽ được gọi trước
python -m unittest -v test_module
3 thì bạn có thể tự gọi
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
47.

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

OK
47 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.1.

Entercontext (cm) ¶addClassCleanup(function, /, *args, **kwargs)

Nhập Trình quản lý bối cảnh được cung cấp. Nếu thành công, cũng thêm phương thức

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

OK
39 làm hàm làm sạch bằng
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
36 và trả về kết quả của phương thức
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
41.

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

docleanups ()

Phương pháp này được gọi là vô điều kiện sau
python -m unittest -v test_module
3 hoặc sau
python -m unittest -v test_module
2 nếu
python -m unittest -v test_module
2 sẽ tăng một ngoại lệ.
enterClassContext(cm)

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

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

OK
36. Nếu bạn cần các chức năng dọn dẹp sẽ được gọi trước
python -m unittest -v test_module
3 thì bạn có thể tự gọi
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
47.context manager. If successful, also add its
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
39 method as a cleanup function by
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
50 and return the result of the
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
41 method.

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

docleanups ()doClassCleanups()

Phương pháp này được gọi là vô điều kiện sau

python -m unittest -v test_module
3 hoặc sau
python -m unittest -v test_module
2 nếu
python -m unittest -v test_module
2 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

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

OK
36. Nếu bạn cần các chức năng dọn dẹp sẽ được gọi trước
python -m unittest -v test_module
3 thì bạn có thể tự gọi
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
47.

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

OK
47 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.

docleanups ()

Phương pháp này được gọi là vô điều kiện sau
python -m unittest -v test_module
3 hoặc sau
python -m unittest -v test_module
2 nếu
python -m unittest -v test_module
2 sẽ tăng một ngoại lệ.
unittest.IsolatedAsyncioTestCase(methodName='runTest')

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

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

OK
36. Nếu bạn cần các chức năng dọn dẹp sẽ được gọi trước
python -m unittest -v test_module
3 thì bạn có thể tự gọi
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
47.

docleanups ()

Phương pháp này được gọi là vô điều kiện sau
python -m unittest -v test_module
3 hoặc sau
python -m unittest -v test_module
2 nếu
python -m unittest -v test_module
2 sẽ tăng một ngoại lệ.
asyncSetUp()

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

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

OK
36. Nếu bạn cần các chức năng dọn dẹp sẽ được gọi trước
python -m unittest -v test_module
3 thì bạn có thể tự gọi
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
47.

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

OK
47 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.
asyncTearDown()

classMethodAdDClassCleanup (function, /, *args, ** kwargs) ¶

addasynccleanup (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.

CoroutineenteraSyncContext (cm) ¶enterAsyncContext(cm)

Nhập Trình quản lý bối cảnh không đồng bộ được cung cấp. Nếu thành công, cũng thêm phương thức

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

OK
71 của nó làm hàm làm sạch bằng
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
72 và trả về kết quả của phương pháp
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
73.asynchronous context manager. If successful, also add its
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
71 method as a cleanup function by
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
72 and return the result of the
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
73 method.

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

chạy (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

self.assertRaises(TypeError, o1 < o2)
43 được truyền qua kết quả. Nếu kết quả bị bỏ qua hoặc
python -m unittest -v test_module
8, 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
self.assertRaises(TypeError, o1 < o2)
80) và được sử dụng. Đối tượng kết quả được trả lại cho người gọi ____ 181. 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ự:

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

OK
7

Sau khi chạy thử nghiệm,

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

OK
78 sẽ chứa
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
79.

classunittest.fectiontestcase (testfunc, setup = none, refown = none, description = none) ¶unittest.FunctionTestCase(testFunc, setUp=None, tearDown=None, description=None)

Lớp này thực hiện phần của giao diện

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 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 mã thử nghiệm cũ, cho phép nó được tích hợp vào khung thử nghiệm dựa trên ________ 56.

Bí danh không dùng nữa

Vì lý do lịch sử, một số 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
9 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

classunittest.testsuite (tests = ()) ¶ unittest.TestSuite(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

self.assertRaises(TypeError, o1 < o2)
26 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.

self.assertRaises(TypeError, o1 < o2)
26 Các đối tượng hoạt động giống như các đối tượng
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9, 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
self.assertRaises(TypeError, o1 < o2)
26:

AddTest (kiểm tra) ¶(test)

Thêm

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 hoặc
self.assertRaises(TypeError, o1 < o2)
26 vào bộ.

Bổ sung (Kiểm tra) ¶(tests)

Thêm tất cả các bài kiểm tra từ một 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
9 và
self.assertRaises(TypeError, o1 < o2)
26 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

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

OK
91 cho mỗi yếu tố.

self.assertRaises(TypeError, o1 < o2)
26 chia sẻ các phương pháp sau với
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9:

chạy (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ư

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

OK
94,
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
95 yêu cầu đối tượng kết quả được truyền vào.

gỡ lỗi ()()

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 bài kiểm tra chạy theo trình gỡ lỗi.

CounttestCase ()()

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ẻ.

__iter __ ()()

Các thử nghiệm được nhóm bởi

self.assertRaises(TypeError, o1 < o2)
26 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 đè
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
97. Lưu ý rằng phương pháp này có thể được gọi là 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
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
95 phải giống nhau cho mỗi lần lặp lại. Sau
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
95, 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
00 để 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 đã truy cập trực tiếp thay vì thông qua việc lặp lại, do đó, ghi đè

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

OK
97 không đủ để cung cấp các bài kiểm tra.In earlier versions the
self.assertRaises(TypeError, o1 < o2)
26 accessed tests directly rather than through iteration, so overriding
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
97 wasn’t sufficient for providing tests.

Đã thay đổi trong phiên bản 3.4: Trong các phiên bản trước,

self.assertRaises(TypeError, o1 < o2)
26 đã tổ chức các tài liệu tham khảo cho mỗi
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 sau
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
95. Các lớp con có thể khôi phục hành vi đó bằng cách ghi đè
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
00.In earlier versions the
self.assertRaises(TypeError, o1 < o2)
26 held references to each
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 after
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
95. Subclasses can restore that behavior by overriding
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
00.

Trong cách sử dụng điển hình của đối tượng

self.assertRaises(TypeError, o1 < o2)
26, phương pháp
self.assertRaises(TypeError, o1 < o2)
81 được gọi bởi
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
09 thay vì khai thác thử nghiệm người dùng cuối.

Đang tải và chạy thử nghiệm

classunittest.testloader¶unittest.TestLoader

Lớp

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

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

OK
10 đượ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
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 cung cấp một thể hiện có thể được chia sẻ là
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
12. 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
10 có các thuộc tính sau:

lỗi

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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
10 có các phương pháp sau:

LoadTestSFromTestCase (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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
16 có nguồn gốc từ ____ 59.

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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
17. Theo mặc định, đây là các tên phương thức bắt đầu bằng
python -m unittest tests/test_something.py
6. Nếu
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
17 trả về không có phương thức, nhưng phương thức
self.assertRaises(TypeError, o1 < o2)
65 đượ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ế.

LoadTestSfrayule (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ừ

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 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 ____ 59 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23, 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23.

Đã thay đổi trong phiên bản 3.2: Hỗ trợ cho

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

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

OK
23 được thêm vào.Support for
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23 as the third argument.

LoadTestSFromName (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

self.assertRaises(TypeError, o1 < o2)
26 hoặc đối tượng có thể gọi được trả về một phiên bản
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 hoặc
self.assertRaises(TypeError, o1 < o2)
26. 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ột mô-đun

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

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

OK
30 chứa lớp
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
32 có nguồn gốc từ ____ 59 với ba phương pháp thử nghiệm (
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
33,
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
34 và
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
35), trình xác nhận
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
36 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
37 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
34. 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
39 hoặc
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
40 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
39 or
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
40 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.

LoadTestSFromNames (Tên, Mô -đun = Không) ¶(names, module=None)

Tương tự như

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

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

OK
41, nhưng có một chuỗi các tên chứ không phải là một tên. 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.

gettestcasenames (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

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

Khám phá (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

self.assertRaises(TypeError, o1 < o2)
47 đượ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

self.assertRaises(TypeError, o1 < o2)
05) được tìm thấy, gói sẽ được kiểm tra chức năng
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23. Nếu điều này tồn tại thì nó sẽ được gọi là
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
46. Thử nghiệm khám phá chăm sóc để đả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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
47.

Nếu

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

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

OK
23 tồn tại thì Discovery không tái phát vào gói,
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23 không cần chuyển đối số này trong
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
51.

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

self.assertRaises(TypeError, o1 < o2)
47 khi nhập được ghi là bỏ qua, không phải lỗi.Modules that raise
self.assertRaises(TypeError, o1 < o2)
47 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23 regardless of whether their path matches pattern, because it is impossible for a package name to match the default pattern.

Đã thay đổi trong phiên bản 3.11: start_dir không thể là một gói không gian tên. Nó đã bị phá vỡ kể từ Python 3.7 và Python 3.11 chính thức loại bỏ nó.start_dir can not be a namespace packages. It has been broken since Python 3.7 and Python 3.11 officially remove it.

Các thuộc tính sau của

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

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

OK
10 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:

testmethodprefix¶

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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
55.

Điều này ảnh hưởng đến

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

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

OK
17 và tất cả các phương pháp
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
57.

SortTestMethodsusing¶

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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
17 và tất cả các phương thức
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
57.

Suiteclass¶

Đố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

self.assertRaises(TypeError, o1 < o2)
26.

Điều này ảnh hưởng đến tất cả các phương pháp

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

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

OK
57.

testNamePotye

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 -v test_module
5).

Nếu thuộc tính này không phải là

python -m unittest -v test_module
8 (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
1, do đó, không giống như các mẫu được truyền đến tùy chọn
python -m unittest -v test_module
5, 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
cd project_directory
python -m unittest discover
0.

Điều này ảnh hưởng đến tất cả các phương pháp

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

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

OK
57.

testNamePotye

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 -v test_module
5).
unittest.TestResult

Nếu thuộc tính này không phải là

python -m unittest -v test_module
8 (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
1, do đó, không giống như các mẫu được truyền đến tùy chọn
python -m unittest -v test_module
5, 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
cd project_directory
python -m unittest discover
0.

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

classunittest.testresult¶

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
self.assertRaises(TypeError, o1 < o2)
43 lưu trữ kết quả của một tập hợp các thử nghiệm. Các lớp
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 và
self.assertRaises(TypeError, o1 < o2)
26 đả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

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 có thể muốn truy cập vào đối tượng
self.assertRaises(TypeError, o1 < o2)
43 đượ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ụ
self.assertRaises(TypeError, o1 < o2)
43 được trả về bằng phương pháp
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
74 cho mục đích này.

self.assertRaises(TypeError, o1 < o2)
43 Các phiên bản có các thuộc tính sau sẽ được quan tâm khi kiểm tra kết quả chạy một bộ thử nghiệm:

lỗi

Một danh sách chứa 2 bộ của 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
9 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ờ.

thất bại trong

Một danh sách chứa 2 bộ của 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
9 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
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
78.

bỏ qua

Một danh sách chứa 2 bộ của các trường hợp và chuỗi

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 giữ lý do bỏ qua bài kiểm tra.

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

mong đợi

Một danh sách chứa 2 bộ của 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
9 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.

bất ngờ

Một danh sách chứa 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
9 được đánh dấu là những thất bại dự kiến, nhưng đã thành công.

nên dừng lại¶

Đượ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
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
83.

testsrun¶

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

Tổng số bài kiểm tra chạy cho đến nay.

đệm¶

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

Nếu được đặt thành true,
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
84 và
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
85 sẽ được đệm ở giữa
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
86 và
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
87 được gọi. Đầu ra được thu thập sẽ chỉ được lặp lại trên thực tế
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
84 và
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
85 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.

thất bại trong

Nếu được đặt thành true

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

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

OK
83 sẽ được gọi vào lỗi hoặc lỗi đầu tiên, tạm dừng lần chạy thử.

tb_locals¶()

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.()

đã thành công()¶

Ví dụ: tính năng này được sử dụng bởi lớp

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

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

OK
96 để dừng khung kiểm tra khi người dùng báo hiệu một ngắt từ bàn phím. Các công cụ tương tác cung cấp triển khai
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
09 có thể sử dụng điều này theo cách tương tự.

Các phương pháp sau đây của lớp

self.assertRaises(TypeError, o1 < o2)
43 đượ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.

starttest (kiểm tra) ¶(test)

Được gọi khi thử nghiệm thử nghiệm sắp được chạy.

stoptest (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ả.

starttestrun () ¶()

Đượ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.

stoptestrun () ¶()

Đượ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.

stoptestrun () ¶(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.

adderror (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
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
99:
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
00.
(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
01 vào thuộc tính của thuộc tính ____ ____502, trong đó Formated_err là một dấu vết được định dạng có nguồn gốc từ ERR.

addfailure (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
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
99:
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
04.
(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
01 vào thuộc tính của thuộc tính ____ ____506, trong đó định dạng_err là một dấu vết được định dạng có nguồn gốc từ ERR.

Thêm thành công (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ì.

addskip (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
07 vào thuộc tính của
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
08.

addExpectedFailure (test, 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í
self.assertRaises(TypeError, o1 < o2)
49.
(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
01 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
11, trong đó Formated_err là một dấu vết được định dạng có nguồn gốc từ ERR.

addunExpectionsuccess (kiểm tra) ¶

Được gọi là khi thử nghiệm thử nghiệm được đánh dấu bằng người trang trí
self.assertRaises(TypeError, o1 < o2)
49, 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

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

addsubtest (kiểm tra, phụ 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 ____59 mô tả bài kiểm tra nhất.

Nếu kết quả là

python -m unittest -v test_module
8, 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
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
99:
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
04.

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.unittest.TextTestResult(stream, descriptions, verbosity)

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

classunittest.texttestresult (stream, mô tả, 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
20. The old name still exists as an alias but is deprecated.

Một triển khai cụ thể của
self.assertRaises(TypeError, o1 < o2)
43 được sử dụng bởi
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
96.
defaultTestLoader

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
20. Tên cũ vẫn tồn tại dưới dạng bí danh nhưng không được dùng.

Unittest.DefaultTestloader¶unittest.TextTestRunner(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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
10 dự định được chia sẻ. Nếu không cần tùy chỉnh
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
10, trường hợp này có thể được sử dụng thay vì liên tục tạo ra các trường hợp mới.

classunittest.texttestrunner (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
30 or
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
31, 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
32 or
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
33 options (see Warning control) and leaving warnings to
python -m unittest -v test_module
8.

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à

python -m unittest -v test_module
8, mặc định,
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
85 đượ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
25 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
35 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
26,
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
27,
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
28 và
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
29 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
30 hoặc
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
31, chúng sẽ chỉ xuất hiện một lần mỗi 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ách sử dụng các tùy chọn Python từ ____532 hoặc
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
33 (xem kiểm soát cảnh báo) và để lại cảnh báo cho
python -m unittest -v test_module
8.The default stream is set to
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
85 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
35.Added the tb_locals parameter.

Đã thay đổi trong phiên bản 3.2: Luồng mặc định được đặt thành
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
85 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.

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
40 khởi tạo lớp hoặc có thể gọi được được thông qua trong hàm tạo
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
96 làm đối số
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
42. Nó mặc định là
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
34 nếu không có
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
42 được cung cấp. Lớp kết quả được khởi tạo với các đối số sau:

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

OK
8

chạy (kiểm tra) ¶(test)

Phương pháp này là giao diện công cộng chính cho

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

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

OK
96. Phương pháp này có một ví dụ
self.assertRaises(TypeError, o1 < o2)
26 hoặc
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9. A
self.assertRaises(TypeError, o1 < o2)
43 được tạo bằng cách gọi
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
40 và (các) bài kiểm tra được chạy và kết quả được in thành stdout.

unittest.main (module = '__ main__', defaultTest = none Không có)¶main(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:

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

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

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

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

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

python -m unittest -v test_module
8 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

python -m unittest -v test_module
8, các giá trị của
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
52 đượ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 test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
53 gọi
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
54 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
10 và mặc định là
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
56.

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
53 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 test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
58. Đ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 test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
54:

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

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

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

python -m unittest -v test_module
8 nếu tùy chọn
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
61 được chuyển cho 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
30.warning filter that should be used while running the tests. If it’s not specified, it will remain
python -m unittest -v test_module
8 if a
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
61 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
30.

Gọi

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
53 thực sự trả về một thể hiện của lớp
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
64. Đ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 test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
65.

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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23.

Nếu một mô -đun thử nghiệm xác định

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

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

OK
23, nó sẽ đượ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
68 với các đối số sau:

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

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

OK
2

trong đó mẫu được truyền thẳng từ

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
69. Nó mặc định là
python -m unittest -v test_module
8.

Nó sẽ trả lại một

self.assertRaises(TypeError, o1 < o2)
26.

Trình tải là ví dụ của

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

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

OK
10 đang 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23 điển hình tải các bài kiểm tra từ một tập hợp các lớp
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 cụ thể có thể trông giống như:

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

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

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

python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
1, thì gói
self.assertRaises(TypeError, o1 < o2)
05 sẽ được kiểm tra
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23. 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23 được gọi với các đối số sau:

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

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

OK
2

Điều này sẽ trả về

self.assertRaises(TypeError, o1 < o2)
26 đại diện cho tất cả các thử nghiệm từ gói. (
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
80 sẽ chỉ chứa các thử nghiệm được thu thập từ
self.assertRaises(TypeError, o1 < o2)
05.)

Bởi vì mẫu được chuyển vào

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

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

OK
23, 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.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
23 cho gói thử nghiệm sẽ giống như:

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

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

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

self.assertRaises(TypeError, o1 < o2)
26. Khi bộ kiểm tra gặp phải một bài kiểm tra từ một lớp mới thì
self.assertRaises(TypeError, o1 < o2)
55 từ lớp trước (nếu có) được gọi, theo sau là
self.assertRaises(TypeError, o1 < o2)
54 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 test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
87 từ mô -đun trước được chạy, theo sau là
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
88 từ mô -đun mới.

Sau khi tất cả các bài kiểm tra đã chạy cuối cùng

self.assertRaises(TypeError, o1 < o2)
76 và
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
87 đượ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

self.assertRaises(TypeError, o1 < o2)
74 /
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
88 (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 test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
93 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 test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
94 (có giao diện tương tự như
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9) đượ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:

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

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

OK
6

Nếu bạn muốn

self.assertRaises(TypeError, o1 < o2)
74 và
self.assertRaises(TypeError, o1 < o2)
76 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
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 trống.

Nếu một ngoại lệ được nâng lên trong một

self.assertRaises(TypeError, o1 < o2)
74 thì các bài kiểm tra trong lớp không được chạy và
self.assertRaises(TypeError, o1 < o2)
76 không được chạy. Các lớp bị bỏ qua sẽ không có
self.assertRaises(TypeError, o1 < o2)
74 hoặc
self.assertRaises(TypeError, o1 < o2)
76 chạy. Nếu ngoại lệ là ngoại lệ
self.assertRaises(TypeError, o1 < o2)
47 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:

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

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

OK
7

Nếu một ngoại lệ được nêu trong một

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
88 thì không có thử nghiệm nào trong mô -đun sẽ được chạy và
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
87 sẽ không được chạy. Nếu ngoại lệ là ngoại lệ
self.assertRaises(TypeError, o1 < o2)
47 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
07:

unittest.addmodulecleanup (function, /, *args, ** kwargs) ¶addModuleCleanup(function, /, *args, **kwargs)

Thêm một chức năng để được gọi theo sau

self.assertRaises(TypeError, o1 < o2)
57 để dọn dẹp tài nguyên đượ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
09 khi chúng được thêm vào.

Nếu

self.assertRaises(TypeError, o1 < o2)
56 thất bại, có nghĩa là
self.assertRaises(TypeError, o1 < o2)
57 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.

classmethodunittest.entermodulecontext (cm) ¶unittest.enterModuleContext(cm)

Nhập Trình quản lý bối cảnh được cung cấp. Nếu thành công, cũng thêm phương thức

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

OK
39 làm hàm làm sạch bằng
python -m unittest tests/test_something.py
09 và trả về kết quả của phương thức
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
41.context manager. If successful, also add its
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
39 method as a cleanup function by
python -m unittest tests/test_something.py
09 and return the result of the
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
41 method.

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

unittest.domodulecleanups () ¶doModuleCleanups()

Hàm này được gọi là vô điều kiện sau

self.assertRaises(TypeError, o1 < o2)
57 hoặc sau
self.assertRaises(TypeError, o1 < o2)
56 nếu
self.assertRaises(TypeError, o1 < o2)
56 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
09. Nếu bạn cần các chức năng dọn dẹp sẽ được gọi trước
self.assertRaises(TypeError, o1 < o2)
57 thì bạn có thể tự gọi
python -m unittest tests/test_something.py
20.

python -m unittest tests/test_something.py
20 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.

classmethodunittest.entermodulecontext (cm) ¶

Nhập Trình quản lý bối cảnh được cung cấp. Nếu thành công, cũng thêm phương thức

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

OK
39 làm hàm làm sạch bằng
python -m unittest tests/test_something.py
09 và trả về kết quả của phương thức
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
41.

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

unittest.domodulecleanups () ¶

Hàm này được gọi là vô điều kiện sau

self.assertRaises(TypeError, o1 < o2)
57 hoặc sau
self.assertRaises(TypeError, o1 < o2)
56 nếu
self.assertRaises(TypeError, o1 < o2)
56 tăng một ngoại lệ.

Unittest.InstallHandler () ¶installHandler()

Cài đặt bộ xử lý Control-C. Khi nhận được

python -m unittest tests/test_something.py
26 (thường là phản hồi với người dùng nhấn Control-C), tất cả các kết quả đã đăng ký đã được gọi là
test_isupper (__main__.TestStringMethods.test_isupper) ... ok
test_split (__main__.TestStringMethods.test_split) ... ok
test_upper (__main__.TestStringMethods.test_upper) ... ok

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

OK
83.

unittest.registerresult (kết quả) ¶registerResult(result)

Đăng ký một đối tượng

self.assertRaises(TypeError, o1 < o2)
43 để xử lý điều khiển-C. Đăng ký kết quả lưu trữ một tham chiếu yếu đến nó, vì vậy nó không ngăn được kết quả không được thu thập rác.

Đăng ký một đối tượng

self.assertRaises(TypeError, o1 < o2)
43 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 liệu xử lý có được bật hay không.

unittest.removeresult (kết quả) ¶removeResult(result)

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

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

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

OK
83 sẽ không còn được gọi trên đối tượng kết quả đó để đáp ứng với một điều khiển-C.

unittest.removeHandler (function = none) ¶removeHandler(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:

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

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

OK
8