python testing framework pytest

One:

pytest testing framework is a set of python fully functional advantages as follows:

1, the operation is simple, supports a plurality of sets of data parameters, and skip support xfail use cases;

2, unit testing support simple and complex functional tests, you can also do UI test automation and interfaces;

3, pytest there are many third-party plug-ins and support for custom extensions; such as failure to perform again, an assertion failure also continue to run custom error stop, run custom mark marking flexible use cases ....

4, can be well integrated CI

two:  

Framework:

A similar setup, teardown is also more flexible,

Module level (setup_module / teardown_module) module whole story, globally

Useful function-level function (setup_function / teardown_function) is not in the class

Class-level (setup_class / teardown_class) run only once before and after classes.

Method stage (setup_method / teardown_methond) runs the whole story class method

session () across file-level

three:

Pytest invocation and case design principles:

1. Pytest / py.test (terminal, the command line, the line PyCharm are configurable PyCharm

Performed using pytest way)

Pytest -v (the highest level of information -verbose -q silent mode)

pytest -v -s -q filename (s console output is a result of zone output is also detailed) 2. pytest will run test _ * .py * _test.py form or in the current directory and its subdirectories

3. beginning test_ function, start with Test classes, beginning test_ method. All packages package must have a __init__.py file.

4. Pytest embodiments and methods can be performed in the frame written unittest

 

four:

Fixture: For setup and teardown some scenes do not apply, the same class of use cases and some need to do some setup does not require the use case use case; this time with a fixture relatively easy to use.

usage:

1, for setup, teardown, can not afford these two names, so named flexible manner. (Independently named after the declaration activation)

2, data sharing. Write method in conftest.py configuration where you can share data, you do not need to import import. Across file sharing, file and use conftest file in the same folder;

3, scope and level of the magical combination of yield is equivalent to a variety of setup and teardown of all files.

 

Do not want any changes to the original test methods, or all automatically applied automatically, without exception, you can choose to automatically apply when the return value is also not required

Solution: Use fixture parameter autouse = True achieve

Step: (autouse = True) used in the method of the above plus @ pytest.mark.usefixtures @ pytest.fixture

Step: On the test methods plus @ pytest.mark.usefixtures ( "start")

 

It is inseparable from the test data, data for a flexible, general data are passed through the parameter

Solution: fixture Request transmitted through the fixed parameter;

Step: Add @ pytest.fixture (params = [1, 2, 3, 'linda']) in a fixture; write request in the process parameters

pytest Import 
#module module level, only run the modular once. Range if not, the default will be performed before each method is performed in the module; automatic setting autouse = True after application, all method names do not increase after the function module within rear @ pytest.fixture (scope = "module" , = True autouse) DEF open_browser ():
    print ( "\ n Open a browser and open the Home") 
at the end survive a teardown operation after the yield # module executing the case.
print ( 'execution tearDown')
print ( 'finally close the browser')

DEF test_soso (the Login): #login function written in conftest file pytest.fixture @ () DEF the Login ():
print ( 'Case1: After the implementation of the international board Search ')

DEF test_cakan ():
Print (' Case2: no direct landing operation ')

DEF test_cart (login):
Print (' Case3, after landing make purchases')

without changing the original code: method or decorator adding new features, so that a flexible method after decorated with setup teardown or other functional items
@ pytest.fixture ()
DEF open_browser ():
    print ( "\ n Open a browser and open the Home") 
yield
Print ( 'execution tearDown')
Print ( 'finally close the browser')

@ pytest.mark.usefixtures ( 'open_browser')
DEF test_soso (): #
DEF test_soso ( open_browser ) effect is the same, but this will change the structure of the original code, reuse elsewhere, then it may be a problem
    print ( 'case1: Gordon occasion after performing a search')
Fives:
Common library dependencies:

Pip install pytest-sugar
pip install pytest-rerunfailures Pip install pytest-xdist
Pip install pytest-assume
Pip intall pytest-html

 
six:
Parameterization and data driving:
= test_user_data [ "zhangsan", "lisi"] 

@ pytest.fixture ()
DEF login_r (Request):
the User = request.param
Print ( "\ home ready to open the n-landing, landing user:% S"% the User)
return the User

@ pytest.mark.parametrize ( 'login_r', test_user_data, the INDIRECT = True)
DEF test_login_s (login_r):
Print (login_r)

is equivalent to:
pytest.fixture @ (params = test_user_data) 
DEF login_r (Request):
the User = request.param
Print ( "\ home ready to open the n-landing, landing user:% S"% the User)
return the User

DEF test_login_s (login_r):
Print (login_r )

sets of data:
test_user_data1 = [{"user": "linda", "password": "888888"},
{"user": "servenruby", "password": "123456"},
{"user": "test01", "password": ""}]

test_user_data2 = [{"q": "中国平安", "count": 3, "page": 1},
{"q": "阿里巴巴", "count": 2, "page": 2},
{"q": "pdd", "count": 3, "page": 1}]

@pytest.fixture(scope="module")
def login_r(request):
# 这是接收传入的参数,接收一个参数
user = request.param['user']
password = request.param['password']
print("\n用户名:%s,密码:%s" % (user, password))

@pytest.fixture(scope="module")
def query_param(request):
# This is to receive incoming parameters, receiving a second parameter
    request.param = Q [ 'Q'] 
COUNT = request.param [ 'COUNT']
Page request.param = [ 'Page']
Print ( "word search query:% S"% Q)
return request.param

# This parameter data pytest drive, indeirect = True is the login_r as a function to perform, I will not write it when it is a parameter
# bottom-up execution
# there are two data combination test 3 * 3 test case execution ( * test_user_data2 number of test_user_data1 number)
@ pytest.mark.parametrize ( "query_param", test_user_data2, the INDIRECT = True)
@ pytest.mark.parametrize ( "login_r", test_user_data1, the INDIRECT = True)
DEF test_login (login_r, query_param ):
# landing cases
Print (login_r)
Print (query_param)
















Guess you like

Origin www.cnblogs.com/1026164853qqcom/p/11280684.html