pytest of Python unit testing framework -- assertion

For testing, whether it is functional testing, automated testing, or unit testing. Generally, a correct expected result is preset, and an actual result will be obtained during the test execution. The success of the test is the comparison of the actual results with the expected results. The process of this comparison is actually an assertion.

  Rich assertion methods are provided in the unittest unit test framework, such as assertEqual(), assertIn(), assertTrue(), assertIs(), etc., while the pytest unit test framework does not provide special assertion methods, but directly uses python The assert asserts.

  Now let's introduce the use of assert.

Compare size and equality                      

test_assert.py

#coding=utf-8
import pytest

# 功能
def add(a,b):
    return a + b

# 测试相等
def test_add():
    assert add(3,4) == 7 

# 测试不相等
def test_add2():
    assert add(17,22) != 50

# 测试大于
def test_add3():
    assert add(17,22) <= 50

# 测试小于
def test_add4():
    assert add(17,22) >= 50


if __name__ == '__main__':
    pytest.main("test_assert.py")

    Define an add() function to calculate the addition of two input parameters and return the result of the addition.

  And assert can directly use symbols such as "==", "!=", "<", ">", ">=", "<=" to compare equal, unequal, less than, greater than, greater than or equal to and less than equal.

  operation result:

============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest\test_case, inifile: 
plugins: html
collected 4 items

test_assert.py ...F

================================== FAILURES ===================================
__________________________________ test_add4 __________________________________

    def test_add4():
>       assert add(17,22) >= 50
E    assert 39 >= 50
E     +  where 39 = add(17, 22)

test_assert.py:22: AssertionError
===================== 1 failed, 3 passed in 0.02 seconds ======================

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

test contains or does not contain                                                    

test_assert2.py

#coding=utf-8
import pytest


# 测试相等
def test_in():
    a = "hello"
    b = "he"
    assert b in a 


# 测试不相等
def test_not_in():
    a = "hello"
    b = "hi"
    assert b not in a

if __name__ == '__main__':
    pytest.main("test_assert2.py")

   Compare relations for inclusion by defining a and b string variables.

  assert can directly use in and not in to compare inclusion and non-inclusion.

  operation result:

============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest\test_case, inifile: 
plugins: html
collected 2 items

test_assert2.py F.

================================== FAILURES ===================================
___________________________________ test_in ___________________________________

    def test_in():
        a = "hello"
        b = "hi"
>       assert b in a
E    assert 'hi' in 'hello'

test_assert2.py:9: AssertionError
===================== 1 failed, 1 passed in 0.01 seconds ======================

  Obviously "hello" does not contain "hi", so the first test case fails.

test true or false                         

test_assert3.py

#coding=utf-8
import pytest


#用于判断素数
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
        return True


# 判断是否为素数
def test_true():
    assert is_prime(13)


# 判断是否不为素数
def test_true():
    assert not is_prime(7)

if __name__ == '__main__':
    pytest.main("test_assert3.py")

   Use the is_prime() function to determine whether n is a prime number (a number that can only be divisible by 1 and itself). The return value is true or false.

  The assert does not require any auxiliary symbols to directly judge whether the object is true, while assert not is used to judge whether it is false.

  operation result:

============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest\test_case, inifile: 
plugins: html
collected 1 items

test_assert3.py F

================================== FAILURES ===================================
__________________________________ test_true __________________________________

    def test_true():
>       assert not is_prime(7)
E    assert not True
E     +  where True = is_prime(7)

test_assert3.py:22: AssertionError
========================== 1 failed in 0.01 seconds ===========================

  It shows that for the second test case, 7 is a prime number, so the return result of the is_prime() function is True, and the correct result required by assert not is False, so the execution of the test case fails.

Come on, testers! If you need to improve your plans, do it, it's better to be on the road than to wait and see from the beginning. Your future self will definitely thank your hard-working self now!

  

Guess you like

Origin blog.csdn.net/weixin_47648853/article/details/131295316