stay ( one ) in , Simple right selenium Introduce common problem methods , In the second part, the use of the combined framework is introduced in detail

( one )pytest Introduction of

be based on python Framework commonly used unittest,pytest,rebotframework
unittest,pytest Focus on unit testing
rebotframework Focus on system testing

pytest Can be used to do System test Automation of , Its characteristics are

(1) use Python Write test cases , Easy to use

(2) Can be used File system directory hierarchy corresponding Manual test case hierarchical structure

(3) Flexible initialization clearing mechanism

(4) Flexible selection of test cases for execution

(5) Utilize third-party plug-ins , Can generate good reports

( two ) install

pytest install
pip install pytest
Test report installation
pip install pytest-html
Successful installation interface , input pytest Have proper environment

( three ) establish pytest Basic use case

Official documents Given pytest seek Test item of Specific rules :

(1) If command line parameters are not specified , Then from testpath( If configured ) Or the current directory starts collecting .
If command line parameters , Assigned catalogue , file name or node id Any combination of , Then find by parameters

(2) The search process recurses into the directory , Unless they match norecursedirs.

(3) In these directories , Search for imported by its test package name test_*.py or * file .

(4) From these files , Collect the following test items :
test Is prefix of function
Test Prefixed class Inside test Prefix method

So establish test files, We store test cases in classes , Class name must be Test Class prefixed with , The method corresponding to the use case must be test
Prefix method , otherwise pytest Unrecognized , Enter the following code :
import pytest class Test1: def test_01(self): print('\n Use case C001001') assert 1 ==
1 def test_C001002(self): print('\n Use case C001002') assert 2 == 2 def test_C001003(
self): print('\n Use case C001003') assert 3 == 2
stay terminal One in pytest Simple test of

pytest Use case check directly in python of assert Assertion , The above indicates that two pass , One failed , And indicate the detailed location
If more detailed information is required , Then test class , Test function plus -v; The information to be printed is added -s
So you can
# Specify directory to run pytest test Or multiple directories pytest test test1\test11 # Specify module execution pytest
test\ # Specified class run pytest test\\Test1 # Specify method operation pytest
Run this module pytest -sv

There are also some commonly used ones pytest Parameters such as -k Fuzzy query class name , Function name , Module name runs the specified use case
such as pytest -k ‘Test1’ Execution class Test1

Label certain methods or classes
For example, give test cases 1 And test cases 2 Label , Reference decorator @pytest.mark.webtest
implement pytest -v -m webtest Use cases that execute tags only

( four ) Generate test reports of all test files in the directory

Put the above File in New test In the directory , implement
pytest test --html=report.html --self-contained-html
stay test Generated in directory report.html Test report , Open this html The file can be seen as follows

( five ) Initialize cleanup
The execution sequence hierarchy of initialization and cleanup is as follows
———— Directory initialization ———— Module initialization ———— Class initialization ———— Method initialization ———— Method clear ———— Class cleanup ———— Module clear ———— Directory cleanup
The example code is as follows ( Initialization and clearing function names carry keywords setup,teardown):
def setup(self): print(" Module initialization ") def teardown(self): print(" Module completely cleared ") class Test2
: # Class initialization , What is called here is classmethod @classmethod def setup_class(cls): print("\n Initialization class ")
# Class cleanup @classmethod # Execute at the end of the execution of all use cases def teardown_class(cls): print("\n Class cleared successfully ")
# Method initialization def setup_method(self): print("\n Method initialization ") # Method clear def teardown_method(self
): print('\n Method clear ') def test_01(self): print('\n Use case C001001') assert 1 == 1 def
test_C001002(self): print('\n Use case C001002') assert 2 == 2 def test_C001003(self):
print('\n Use case C001003') assert 3 == 2
Create another directory to initialize clearing , stay test New under file file , The file name should be fixed
import pytest

def st_emptyEnv():
print(' initial directory ') yield print(' clean directories ')
The hierarchy is as follows , implement pytest test -sv

Briefly introduced pytest Basic use of framework , stay python+selenium+pytest( lower ) Carry out a practical automatic test of the project at work

©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?