The pytest of the Python Unit test framework--assertions

Source: Internet
Author: User
Tags assert

 For testing, whether it's a functional test, an automated test, or a unit test. Generally, a correct expected result is preset, and a practical result is obtained during the test execution. The success of the test is to compare the actual results with the expected results. The process of this comparison is actually an assertion (assert).

Rich assertion methods are provided in the UnitTest Unit test framework, such as Assertequal (), Assertin (), Asserttrue (), Assertis (), etc., and the Pytest Unit Test framework does not provide a special assertion method, Instead, assert them directly using the Python assert.

Let's introduce the use of assert.

Compare size to equality

test_assert.py

#Coding=utf-8Importpytest#functiondefAdd (A, b):returnA +b#Test for EqualitydefTest_add ():assertAdd (3,4) = = 7#Test Not Equaldeftest_add2 ():assertAdd (17,22)! = 50#test greater thandeftest_add3 ():assertAdd (17,22) <= 50#Test less thandeftest_add4 ():assertAdd (17,22) >= 50if __name__=='__main__': Pytest.main ("test_assert.py")

defines an Add () function that calculates the sum of two parameters and returns the result of the addition.

Assert can be used to compare equality, inequality, less than, greater than, greater than, and less than equals by using symbols such as "= =", "! =", "<", ">", ">=", "<=", and so on.

Operation Result:

============================= test Session starts =============================Platform Win32--Python 2.7.10--py-1.4.30--pytest-2.7.2Rootdir:d:\pyse\pytest\test_case, inifile:plugins:htmlcollected4itemstest_assert.py ... F================================== Failures ===================================__________________________________Test_add4__________________________________    deftest_add4 ():>assertAdd (17,22) >= 50Eassert50 >=E+ Where is = Add (17, 22) test_assert.py:22: Assertionerror===================== 1 failed, 3 passedinch0.02 seconds ======================

Obviously, the result of 17 plus 22 is not greater than 50, and all the last use cases fail.

Test contains or does not contain

test_assert2.py

#Coding=utf-8Importpytest#Test for Equalitydeftest_in (): a="Hello"b="He"    assertBincha#Test Not Equaldeftest_not_in (): a="Hello"b="Hi"    assertB not inchaif __name__=='__main__': Pytest.main ("test_assert2.py")

Compare the included relationships by defining a and B string variables.

Assert can be used directly to compare inclusions and not contained with in and not.

Operation Result:

============================= test Session starts =============================Platform Win32--Python 2.7.10--py-1.4.30--pytest-2.7.2Rootdir:d:\pyse\pytest\test_case, inifile:plugins:htmlcollected2itemstest_assert2.py F.================================== Failures ===================================___________________________________Test_in___________________________________    deftest_in (): a="Hello"b="Hi">assertBinchAEassert 'Hi' inch 'Hello'test_assert2.py:9: Assertionerror===================== 1 failed, 1 passedinch0.01 seconds ======================

Obviously "Hello" does not contain "HI", so the first test case failed to run.

Test true or False

test_assert3.py

#Coding=utf-8Importpytest#used to determine prime numbersdefis_prime (n):ifN <= 1:        returnFalse forIinchRange (2, N):ifn% i = =0:returnFalsereturnTrue#determine if it is a prime numberdeftest_true ():assertIs_prime (13)#determine if not a prime numberdeftest_true ():assert  notIs_prime (7)if __name__=='__main__': Pytest.main ("test_assert3.py")

The is_prime () function is used to determine whether n is a prime number (it can only be divisible by 1 and itself). The return value is Ture or false.

An assert does not require any auxiliary symbols to directly determine whether the object is ture, and assert not is used to determine whether it is false.

Operation Result:

============================= test Session starts =============================Platform Win32--Python 2.7.10--py-1.4.30--pytest-2.7.2Rootdir:d:\pyse\pytest\test_case, inifile:plugins:htmlcollected1itemstest_assert3.py F================================== Failures ===================================__________________________________Test_true__________________________________    deftest_true ():>assert  notIs_prime (7) Eassert  notTruee+ Where True = Is_prime (7) test_assert3.py:22: Assertionerror========================== 1 failedinch0.01 seconds ===========================

  Shows that for the second Test case, 7 is a prime number, so the return result of the Is_prime () function is ture, and the correct result for assert not is false, so the use case execution fails.

The pytest of the Python Unit test framework--assertions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.