Hướng dẫn python assert not exception - python khẳng định không ngoại lệ

Vì vậy, tôi đang cố gắng nhập một mô -đun và các phương thức kiểm tra từ một lớp trong mô -đun đó.

Dưới đây là một ví dụ về một phương pháp.

def production_warning[self, targetenv]:
    if targetenv == 'prdv':
        prodwarning1 = raw_input["WARNING: You are deploying to the production environment.  Are you sure you want to do this?   Y/N:  "].upper[]
        if prodwarning1 == "N":
            sys.exit[]
        prodwarning2 = raw_input["DEPLOYING TO PRDV, ARE YOU REALLY, REALLY SURE?  Y/N:  "].upper[]
        if prodwarning2 == "N":
            sys.exit[]
    else:
        return True

Đây là một ví dụ về một bài kiểm tra tôi đang cố gắng viết.

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]

Làm cách nào để kiểm tra nếu một lỗi được nêu ra? Vào cuối ngày, tôi đang cố gắng chạy qua tất cả các thử nghiệm này và cho mỗi chức năng, nếu không có ngoại lệ nào được nêu ra, hãy in "thành công". Nếu một ngoại lệ được nâng lên, hãy chuyển sang thử nghiệm tiếp theo. Mọi người dường như tiếp tục chỉ cho tôi theo hướng "gọi chức năng của bạn sẽ tự động tăng một ngoại lệ nếu có", nhưng điều này sẽ ngăn chặn bài kiểm tra của tôi bất cứ khi nào một ngoại lệ bị ném và tôi không muốn điều đó, tôi muốn tiếp tục Bài kiểm tra tiếp theo.

Tôi có thể làm việc xung quanh việc này bằng cách làm:

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]

Nhưng có vẻ như điều đó đánh bại mục đích sử dụng Unittest ngay từ đầu? Tôi đã nghĩ với sự nhất quán, tôi chỉ có thể khẳng định nếu một ngoại lệ được nâng lên và nó trả về một T/F mà tôi có thể làm bất cứ điều gì tôi muốn.

Hy vọng đó là đủ thông tin.

Dựa trên nhiều câu trả lời được đưa ra, tôi cho rằng không có gì được xây dựng để làm việc nhất

unittest.registerresult [kết quả] ¶ Lib/unittest/__init__.py

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
37 để 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.the list of assert methods.]

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
37 không có tác dụng phụ nếu xử lý Control-C không được bật, vì vậy các khung kiểm tra có thể đăng ký vô điều kiện tất cả các kết quả mà chúng tạo ra độc lập về việc xử lý có được bật hay không.

unittest.removeresult [kết quả] ¶

Loại bỏ một kết quả đã đăng ký. Khi kết quả đã được xóa thì
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
77 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] ¶

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

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

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

Khung thử nghiệm đơn vị

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

Để đạt được điều này,
test_isupper [__main__.TestStringMethods.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 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

Xem thêm

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
4

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

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

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

test_isupper [__main__.TestStringMethods.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.

pytest

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

test_isupper [__main__.TestStringMethods.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.

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

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

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

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

Tập lệnh

test_isupper [__main__.TestStringMethods.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 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

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

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

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

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

Các phương thức

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 và
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
7 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 test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
8 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 test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
9 cho tập lệnh kiểm tra của bạn sẽ hướng dẫn
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
8 để 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 ____50 đượ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 tests/test_something.py
2 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 tests/test_something.py
2 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 tests/test_something.py
3 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 [

python -m unittest tests/test_something.py
4] được khớp với tên thử nghiệm bằng cách sử dụng
python -m unittest tests/test_something.py
5; 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ụ:

python -m unittest tests/test_something.py
6 khớp với
python -m unittest tests/test_something.py
7,
python -m unittest tests/test_something.py
8, nhưng không phải
python -m unittest tests/test_something.py
9.

-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

python -m unittest -v test_module
0,
python -m unittest -v test_module
1 và
python -m unittest -v test_module
2 đã được thêm vào.The command-line options
python -m unittest -v test_module
0,
python -m unittest -v test_module
1 and
python -m unittest -v test_module
2 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
3.The command-line option
python -m unittest -v test_module
3.

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

python -m unittest -v test_module
4.The command-line option
python -m unittest -v test_module
4.

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 -v test_module
5, 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 -v test_module
6 là tương đương với
python -m unittest -v test_module
7. Nếu bạn muốn vượt qua các đối số để kiểm tra khám phá, lệnh phụ
python -m unittest -v test_module
8 phải được sử dụng rõ ràng.

Trình lệnh phụ

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

cd project_directory
python -m unittest discover
0]

-P,-statesTpotyn¶,--pattern pattern

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

cd project_directory
python -m unittest discover
1]

-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

cd project_directory
python -m unittest discover
2,
cd project_directory
python -m unittest discover
3 và
cd project_directory
python -m unittest discover
4 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:

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
0

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

cd project_directory
python -m unittest discover
5, 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ụ

cd project_directory
python -m unittest discover
6 sẽ được nhập dưới dạng
cd project_directory
python -m unittest discover
7.

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

cd project_directory
python -m unittest discover
8].Test discovery supports namespace packages for the start directory. Note that you need to specify the top level directory too [e.g.
cd project_directory
python -m unittest discover
8].

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

cd project_directory
python -m unittest discover
9.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
cd project_directory
python -m unittest discover
9 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ụ:

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
1

Tổ chức mã kiểm tra

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

test_isupper [__main__.TestStringMethods.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, các trường hợp thử nghiệm được biểu thị bằng các trường hợ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
9. Để thực hiện các trường hợp kiểm tra của riêng bạn, bạn phải viết các lớp con là
test_isupper [__main__.TestStringMethods.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 hoặc sử dụng
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
03.

Mã thử nghiệm của 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
3 phải hoàn toàn khép kín, sao cho nó có thể được chạy trong sự cô lập hoặc kết hợp tùy ý với bất kỳ số lượng trường hợp thử nghiệm nào khác.

Lớp con

test_isupper [__main__.TestStringMethods.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 giản nhất sẽ chỉ cần thực hiện một phương thức kiểm tra [nghĩa là một phương thức có tên bắt đầu bằng
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
0] để thực hiện mã kiểm tra cụ thể:

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
2

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
07 được cung cấp bởi lớp cơ sở
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 thử nghiệm thất bại, một ngoại lệ sẽ được nêu ra với một thông điệp giải thích và
test_isupper [__main__.TestStringMethods.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 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 ta có thể đưa ra mã thiết lập bằng cách triển khai một phương thức gọi là

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
3

Ghi chú

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

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

Nếu phương thức

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 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 test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
7 phù hợp sau khi phương thức kiểm tra đã được chạy:

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
4

Nếu

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

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

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6,
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
7 và
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
17 sẽ được gọi một lần mỗi lần thử nghiệm.

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

test_isupper [__main__.TestStringMethods.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 cung cấp một cơ chế cho điều này: Bộ thử nghiệm, được biểu thị bằng lớp ____ 50 50 ____ ____120. Trong hầu hết các trường hợp, gọi
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
8 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 đó:

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
5

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ư

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
22], 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ư
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
23:

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

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

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

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

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

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

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

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

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

test_isupper [__main__.TestStringMethods.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, mà không chuyển đổi mọi chức năng kiểm tra cũ thành lớp con
test_isupper [__main__.TestStringMethods.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.

Vì lý do này,

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 cung cấp một lớp
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
03. Lớp con này 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
3 có thể được sử dụng để bọc 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:

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
6

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:

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
7

Ghi chú

Mặc dù

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
03 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 ________ 50, phương pháp này không được khuyến khích. Dành thời gian để thiết lập các lớp con
test_isupper [__main__.TestStringMethods.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 thích hợp sẽ làm cho việc tái cấu trúc thử nghiệm trong tương lai dễ dàng hơn.

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

test_isupper [__main__.TestStringMethods.test_isupper] ... ok
test_split [__main__.TestStringMethods.test_split] ... ok
test_upper [__main__.TestStringMethods.test_upper] ... ok

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

OK
4. Nếu vậy,
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
4 cung cấp một lớp
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
34 có thể tự động xây dựng các trường hợp
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
35 từ các thử nghiệm dựa trên ________ 54 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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
37.

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
38 hoặc một trong những biến thể có điều kiện của nó, gọi
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
39 trong phương pháp
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 hoặc thử nghiệm hoặc tăng trực tiếp
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
41.decorator or one of its conditional variants, calling
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
39 within a
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 or test method, or raising
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
41 directly.

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
8

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
9

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
0

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
42 cũng có thể bỏ qua bài kiểm tra. Điều này rất hữu ích khi một tài nguyên cần được thiết lập không có sẵn.

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
43.

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
1

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
38 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:

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
2

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
39 hoặc một trong những người trang trí bỏ qua thay vì nâng này trực tiếp.

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

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 hoặc
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
7 chạy xung quanh chúng. Các lớp bị bỏ qua sẽ không có
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
48 hoặc
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
49 chạy. Các mô -đun bỏ qua sẽ không có
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
50 hoặc
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
51 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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
52.

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
3

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
4

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
53 sẽ được hiển thị:

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
5

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

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

test_isupper [__main__.TestStringMethods.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.

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

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

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

test_isupper [__main__.TestStringMethods.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 sẽ chạy một phương thức cơ sở duy nhất: phương thức có tên Phương thức. Trong hầu hết các cách sử dụng của
test_isupper [__main__.TestStringMethods.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, bạn sẽ không thay đổi tên phương thức cũng như tái tạo phương thức
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
59 mặc định.

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

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 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
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 từ trình thông dịch tương tá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
3 can be instantiated successfully without providing a methodName. This makes it easier to experiment with
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 from the interactive interpreter.

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
63 hoặc
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
41, 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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
63 hoặc
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
41, được đưa ra bởi phương pháp này sẽ được coi là một lỗi bổ sung thay vì lỗi kiểm tra [do đó làm tăng tổng số lỗi được báo cáo]. Phương pháp này sẽ chỉ được gọi nếu
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 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.

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
68 được gọi với lớp là đối số duy nhất và phải được trang trí là
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
69:

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
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 [] ¶[]

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.

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
70 được gọi với lớp là đối số duy nhất và phải được trang trí dưới dạng
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
69:

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
7

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.

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
70 được gọi với lớp là đối số duy nhất và phải được trang trí dưới dạng
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
69:

chạy [result = none] ¶

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
37 được truyền qua kết quả. Nếu kết quả bị bỏ qua hoặc
python -m unittest tests/test_something.py
2, 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
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
74] và được sử dụng. Đối tượng kết quả được trả lại cho người gọi ____ ____ 175.Previous versions of
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
77 did not return the result. Neither did calling an instance.

Hiệu ứng tương tự có thể có bằng cách gọi đơn giản là phiên bản
test_isupper [__main__.TestStringMethods.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.
[reason]

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

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
77 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 test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
6 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ớ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

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

Kiểm tra điều đó

Mới

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
80

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
81

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
82

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
83

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
84

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
85

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
86

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
87

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
88

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
89

3.1

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
90

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
91

3.1

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
92

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
93

3.1

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
94

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
95

3.1

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
96

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
97

3.1

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
98

def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
99

3.1

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
00

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
01

3.2

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
02

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
03

3.2

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
04]. Lưu ý rằng đối số từ khóa MSG có thể được chuyển đến
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
4,
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
06,
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
07,
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
08 chỉ khi chúng được sử dụng làm trình quản lý ngữ cảnh.

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
09 chức năng bình đẳng cụ thể 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:

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
10 đượ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.
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
10 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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
11 và không với
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
12 [sử dụng
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
13 cho cái sau]. Phương pháp này cũng nên tránh khi có sẵn các phương thức cụ thể hơn [ví dụ:
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
80 thay vì
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
15], 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 tests/test_something.py
2.

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

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

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.

Phương pháp

Kiểm tra điều đó

Mới

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
19

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
04]. Lưu ý rằng đối số từ khóa MSG có thể được chuyển đến
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
4,
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
06,
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
07,
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
08 chỉ khi chúng được sử dụng làm trình quản lý ngữ cảnh.

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
21

assertequal [thứ nhất, thứ hai, msg = none] ¶

3.1

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
23

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

3.2

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
25

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
09 chức năng bình đẳng cụ thể sẽ được gọi để tạo ra Thông báo lỗi mặc định hữu ích [xem thêm danh sách các phương thức cụ thể loại].

3.2

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
27

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

3.4

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
29

Đã thay đổi trong phiên bản 3.2:
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
10 đượ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.

assertNotequal [thứ nhất, thứ hai, msg = none] ¶

3.10

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

assertTrue [expr, msg = none] ¶ assertFalse [expr, msg = none] ¶

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
8

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
11 và không với
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
12 [sử dụng
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
13 cho cái sau]. Phương pháp này cũng nên tránh khi có sẵn các phương thức cụ thể hơn [ví dụ:
def production_warning[]:
    try:
        assert test.production_warning['prdv'] is not errors
        assert test.validate_params['fakeenv'] is errors
        print "Test Passed {0}/5: validate_params".format[counter]
        test_db_refresh[]
    except:
        print "Test Failed {0}/5: validate_params".format[counter]
        test_db_refresh[]

def db_refresh_prompt[]:
    # assert test.db_refresh_prompt[] is not errors
    global counter
    counter += 1
    print "Test Passed {0}/5: db_refresh_prompt".format[counter]

production_warning[]
db_refresh_prompt[]
etc[]
80 thay vì
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
15], 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]

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
9

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

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

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
33 attribute.

assertisNone [expr, msg = none] ¶ assertisnotnone [expr, msg = none] ¶Added the msg keyword argument when used as a context manager.

Kiểm tra rằng expr là [hoặc không]
python -m unittest tests/test_something.py
2.
[exception, regex, callable, *args, **kwds]assertRaisesRegex[exception, regex, *, msg=None]

Giống như

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
4 nhưng cũng kiểm tra rằng Regex khớp với biểu diễn chuỗi của ngoại lệ được nâng lên. 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
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
37. 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[]
0

or:

import unittest

class TestStringMethods[unittest.TestCase]:

    def test_upper[self]:
        self.assertEqual['foo'.upper[], 'FOO']

    def test_isupper[self]:
        self.assertTrue['FOO'.isupper[]]
        self.assertFalse['Foo'.isupper[]]

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

if __name__ == '__main__':
    unittest.main[]
1

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
38.Added under the name
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
38.

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

AssertWarns [cảnh báo, có thể gọi được, *args, ** kwds] ¶ AssertWarns [cảnh báo, *, msg = none][warning, callable, *args, **kwds]assertWarns[warning, *, msg=None]

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
07. Bài kiểm tra vượt qua nếu cảnh báo được kích hoạt và thất bại nếu nó không phải là. Bất kỳ ngoại lệ là một lỗi. Để bắt bất kỳ nhóm cảnh báo nào, một tuple chứa các lớp cảnh báo có thể được thông qua dưới dạng cảnh báo.

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

import unittest

class TestStringMethods[unittest.TestCase]:

    def test_upper[self]:
        self.assertEqual['foo'.upper[], 'FOO']

    def test_isupper[self]:
        self.assertTrue['FOO'.isupper[]]
        self.assertFalse['Foo'.isupper[]]

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

if __name__ == '__main__':
    unittest.main[]
2

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
07 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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
41 của nó và dòng nguồn đã kích hoạt các cảnh báo trong các thuộc tính
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
42 và
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
43. Điều này có thể hữu ích nếu ý định là thực hiện kiểm tra bổ sung về cảnh báo bị bắt:

import unittest

class TestStringMethods[unittest.TestCase]:

    def test_upper[self]:
        self.assertEqual['foo'.upper[], 'FOO']

    def test_isupper[self]:
        self.assertTrue['FOO'.isupper[]]
        self.assertFalse['Foo'.isupper[]]

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

if __name__ == '__main__':
    unittest.main[]
3

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.

AssertWarns [cảnh báo, có thể gọi được, *args, ** kwds] ¶ AssertWarns [cảnh báo, *, msg = none][warning, regex, callable, *args, **kwds]assertWarnsRegex[warning, regex, *, msg=None]

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
07. Bài kiểm tra vượt qua nếu cảnh báo được kích hoạt và thất bại nếu nó không phải là. Bất kỳ ngoại lệ là một lỗi. Để bắt bất kỳ nhóm cảnh báo nào, một tuple chứa các lớp cảnh báo có thể được thông qua dưới dạng cảnh báo.

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

or:

import unittest

class TestStringMethods[unittest.TestCase]:

    def test_upper[self]:
        self.assertEqual['foo'.upper[], 'FOO']

    def test_isupper[self]:
        self.assertTrue['FOO'.isupper[]]
        self.assertFalse['Foo'.isupper[]]

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

if __name__ == '__main__':
    unittest.main[]
5

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.

AssertWarns [cảnh báo, có thể gọi được, *args, ** kwds] ¶ AssertWarns [cảnh báo, *, msg = none][logger=None, level=None]

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
07. Bài kiểm tra vượt qua nếu cảnh báo được kích hoạt và thất bại nếu nó không phải là. Bất kỳ ngoại lệ là một lỗi. Để bắt bất kỳ nhóm cảnh báo nào, một tuple chứa các lớp cảnh báo có thể được thông qua dưới dạng cảnh báo.

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

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
07 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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
41 của nó và dòng nguồn đã kích hoạt các cảnh báo trong các thuộc tính
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
42 và
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
43. Đ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:

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.

AssertWarnsRegex [Cảnh báo, Regex, Callable, *Args, ** KWDS]

Giống như
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
07 nhưng cũng kiểm tra rằng Regex phù hợ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
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
37. Thí dụ:

AssertLogs [logger = none, level = none] ¶

Example:

import unittest

class TestStringMethods[unittest.TestCase]:

    def test_upper[self]:
        self.assertEqual['foo'.upper[], 'FOO']

    def test_isupper[self]:
        self.assertTrue['FOO'.isupper[]]
        self.assertFalse['Foo'.isupper[]]

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

if __name__ == '__main__':
    unittest.main[]
6

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
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
46 hoặc
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
47 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ữ.
[logger=None, level=None]

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
48 hoặc
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
49]. Mặc định là
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
50.

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
28 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.

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
07 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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
41 của nó và dòng nguồn đã kích hoạt các cảnh báo trong các thuộc tính
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
42 và
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
43. Đ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:

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.

AssertWarnsRegex [Cảnh báo, Regex, Callable, *Args, ** KWDS]

Giống như

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
07 nhưng cũng kiểm tra rằng Regex phù hợ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
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
37. Thí dụ:

AssertLogs [logger = none, level = none] ¶

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
60

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
61

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
62

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
63

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
64

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
65

3.1

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
66

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
67

3.1

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
68

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
69

3.1

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
70

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
71

3.1

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
72

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
73

3.1

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
74

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
75

3.2

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
76

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.

3.2

Nếu được đưa ra, logger phải là đối tượng
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
46 hoặc
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
47 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ữ.
[first, second, places=7, msg=None, delta=None]assertNotAlmostEqual[first, second, places=7, msg=None, delta=None]

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
48 hoặc
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
49]. Mặc định là
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
50.

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
28 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.

Cung cấp cả đồng bằng và địa điểm làm tăng

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
78.

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

def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
79 tự động xem xét các đối tượng gần như bằng nhau so sánh bằng nhau.
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
80 tự động thất bại nếu các đối tượng so sánh bằng nhau. Đã thêm đối số từ khóa Delta.
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
79 automatically considers almost equal objects that compare equal.
def validate_params[]:
try:
    assert test.validate_params['hackenv-re', 'test.username'] is not errors
    assert test.validate_params['fakeenv', 'test.username'] is errors
    assert test.validate_params['hackevn-re', 'vagrant'] is errors
    global counter
    counter += 1
    print "Test Passed {0}/5: validate_params".format[counter]
    test_db_refresh[]
except:
    print "Test Failed {0}/5: validate_params".format[counter]
    test_db_refresh[]
80 automatically fails if the objects compare equal. Added the delta keyword argument.

AssertGreater [thứ nhất, thứ hai, msg = none][first, second, msg=None]assertGreaterEqual[first, second, msg=None]assertLess[first, second, msg=None]assertLessEqual[first, second, msg=None]

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

Bài Viết Liên Quan

Chủ Đề