Python bắt đầu với

Khung thử nghiệm đơn vị

python -m unittest tests/test_something.py
9 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 cho các thử nghiệm, tổng hợp các thử nghiệm thành các bộ sưu tập và tính độc lập của các thử nghiệm với khung báo cáo

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

python -m unittest tests/test_something.py
9 hỗ trợ một số khái niệm quan trọng theo cách hướng đối tượng

lịch thi đấu

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

trường hợp thử nghiệm

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ể đối với một bộ đầu vào cụ thể.

python -m unittest tests/test_something.py
9 cung cấp một lớp cơ sở,
python -m unittest -v test_module
3, có thể được sử dụng để tạo các trường hợp thử nghiệm mới

bộ kiểm tra

Bộ kiểm thử là tập hợp các trường hợp kiểm thử, bộ kiểm thử 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ử

Trình chạy thử nghiệm là một thành phần điều phối việc thực hiện các thử nghiệm 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ề một giá trị đặc biệt để chỉ ra kết quả thực hiện các bài kiểm tra

Xem thêm

Mô-đun
python -m unittest -v test_module
4

Another test-support module with a very different flavor

Simple Smalltalk Testing. With Patterns

Kent Beck’s original paper on testing frameworks using the pattern shared by

python -m unittest tests/test_something.py
9

pytest

Third-party unittest framework with a lighter-weight syntax for writing tests. For example,

python -m unittest -v test_module
6

The Python Testing Tools Taxonomy

An extensive list of Python testing tools including functional testing frameworks and mock object libraries

Testing in Python Mailing List

A special-interest-group for discussion of testing, and testing tools, in Python

The script

python -m unittest -v test_module
7 in the Python source distribution is a GUI tool for test discovery and execution. This is intended largely for ease of use for those new to unit testing. For production environments it is recommended that tests be driven by a continuous integration system such as Buildbot, Jenkins, GitHub Actions, or AppVeyor

Basic example¶

The

python -m unittest tests/test_something.py
9 module provides a rich set of tools for constructing and running tests. This section demonstrates that a small subset of the tools suffice to meet the needs of most users

Here is a short script to test three string methods

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

A testcase is created by subclassing

python -m unittest -v test_module
9. The three individual tests are defined with methods whose names start with the letters
python -m unittest tests/test_something.py
10. This naming convention informs the test runner about which methods represent tests

The crux of each test is a call to

python -m unittest tests/test_something.py
11 to check for an expected result;
python -m unittest tests/test_something.py
12 or
python -m unittest tests/test_something.py
13 to verify a condition; or
python -m unittest tests/test_something.py
14 to verify that a specific exception gets raised. These methods are used instead of the
python -m unittest tests/test_something.py
15 statement so the test runner can accumulate all test results and produce a report

The

python -m unittest tests/test_something.py
16 and
python -m unittest tests/test_something.py
17 methods allow you to define instructions that will be executed before and after each test method. They are covered in more detail in the section Organizing test code .

The final block shows a simple way to run the tests.

python -m unittest tests/test_something.py
18 cung cấp giao diện dòng lệnh cho tập lệnh thử nghiệm. When run from the command line, the above script produces an output that looks like this

python -m unittest tests/test_something.py
0

Passing the

python -m unittest tests/test_something.py
19 option to your test script will instruct
python -m unittest tests/test_something.py
18 to enable a higher level of verbosity, and produce the following output

python -m unittest tests/test_something.py
3

The above examples show the most commonly used

python -m unittest tests/test_something.py
9 features which are sufficient to meet many everyday testing needs. The remainder of the documentation explores the full feature set from first principles

Changed in version 3. 11. The behavior of returning a value from a test method [other than the default

python -m unittest tests/test_something.py
52 value], is now deprecated.

Giao diện dòng lệnh¶

The unittest module can be used from the command line to run tests from modules, classes or even individual test methods

python -m unittest tests/test_something.py
6

You can pass in a list with any combination of module names, and fully qualified class or method names

Test modules can be specified by file path as well

python -m unittest tests/test_something.py

This allows you to use the shell filename completion to specify the test module. The file specified must still be importable as a module. The path is converted to a module name by removing the ‘. py’ and converting path separators into ‘. ’. If you want to execute a test file that isn’t importable as a module you should execute the file directly instead

Bạn có thể chạy thử nghiệm với nhiều chi tiết hơn [độ chi tiết cao hơn] bằng cách chuyển vào cờ -v

python -m unittest -v test_module

When executed without arguments Test Discovery is started.

python -m unittest tests/test_something.py
1

For a list of all the command-line options

python -m unittest tests/test_something.py
5

Đã 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ẻ chứ không phải mô-đun hoặc lớp.

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

unittest hỗ trợ các tùy chọn dòng lệnh này

-b, --bộ đệm

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

-c, --bắt

Control-C during the test run waits for the current test to end and then reports all the results so far. A second Control-C raises the normal

python -m unittest tests/test_something.py
53 exception

Xem Xử lý tín hiệu để biết các chức năng cung cấp chức năng này

-f, --không thành công

Dừng chạy thử khi có lỗi hoặc lỗi đầu tiên

-k

Chỉ chạy thử nghiệm các phương thức và 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 đã cho đều được bao gồm

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

python -m unittest tests/test_something.py
54] được khớp với tên bài kiểm tra bằng cách sử dụng
python -m unittest tests/test_something.py
55;

Các mẫu được so khớp với tên phương thức kiểm tra đủ điều kiện do trình tải kiểm tra nhập vào

Ví dụ:

python -m unittest tests/test_something.py
56 khớp với
python -m unittest tests/test_something.py
57,
python -m unittest tests/test_something.py
58, nhưng không khớp với
python -m unittest tests/test_something.py
59

--người dân địa phương

Hiển thị các biến cục bộ trong truy nguyên

Mới trong phiên bản 3. 2. The command-line options

python -m unittest -v test_module
80,
python -m unittest -v test_module
81 and
python -m unittest -v test_module
82 were added.

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

python -m unittest -v test_module
83.

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

python -m unittest -v test_module
84.

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ỉ một tập hợp con

Test Discovery¶

Mới trong phiên bản 3. 2

Unittest hỗ trợ khám phá thử nghiệm đơn giản. In order to be compatible with test discovery, all of the test files must be 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 -v test_module
85, 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à

python -m unittest -v test_module
8

Ghi chú

Là một phím tắt,

python -m unittest -v test_module
86 tương đương với
python -m unittest -v test_module
87. Nếu bạn muốn truyền đối số để kiểm tra khám phá, lệnh phụ
python -m unittest -v test_module
88 phải được sử dụng rõ ràng

Lệnh phụ

python -m unittest -v test_module
88 có các tùy chọn sau

-v, --dài dòng

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

-s, --thư mục bắt đầu thư mục

Thư mục để bắt đầu khám phá [

python -m unittest -v test_module
90 mặc định]

-p, --mẫu mẫu

Chủ Đề