pytest notes

Third, the installation pytest dependent libraries

pip install pytest

pip install pytest-html

pip install paramiko

pip install rpyc

pip install request

Fourth, the use pytest

1. Profiles

pytest.ini is pytest main configuration file, you can change the default behavior pytest, according to the manner specified to run. Pytest.ini basic format:

# Save the file as pytest.ini

[Pytest]

addopts = -rsxX

pytest.ini test scripts can be placed in the directory or by command line parameter to specify the pytest call:

pytest  -c "E:\Desktop\pytest_local.ini"

Not recommended to use the command line parameter to specify the way, because after the -c parameter, the original pytest.ini in the test script in the root directory on the default is not enabled, pytest will use current folder as rootdir, undermines the general sense the root of our test script is fixed notion directory pytest.ini lies. Of course, we can develop command-line parameters to specify the run --- rootdir root directory:

testcase\dir_level1\dir_level2>pytest -c "E:\Desktop\pytest_local.ini" --rootdir="E:\Desktop\Sublime Text Build_x64\test_ini"

pytest method of determining rootdir see Manual:

https://docs.pytest.org/en/latest/customize.html#initialization-determining-rootdir-and-inifile

2. Configuration Item

In this section we will introduce common configuration item, other configuration items, please consult the official manual.

adopts

addopts parameters can change the default command-line option, equivalent to the time we went to perform the use case plus the input parameters in the cmd command, such as specifying the log file directory information and the generated test bed:

[Pytest]

addopts = --testbed="E:\Desktop\topology_template.yaml" --reportpath="E:\Desktop"

--testbed

Command line parameters specifying the test bed, or may be written manually addopts output time of the call, the parameter is mandatory.

--reportpath

Specify the root directory for the log file is generated, if not selected, the log file is selected by default pytest of rootdir as the root directory of the log

python_functions

No matching test with example numbers beginning with a generally fixed, such as in the beginning TestCase_, thus configured TestCase_

python_files

Match test script file name, TestCase script names are TestCase_ beginning, so configured TestCase_ *

In summary, pytest.ini sample we placed in the root directory of the following subsystems can be modified according to their needs when the script runs.

# pytest.ini

[Pytest]

addopts = -s --testbed="E:\Desktop\topology_template.yaml" --reportpath="E:\Desktop"

python_files = TestCase_*

python_functions = TestCase_*

The following describes some common configuration items

markers

In order to avoid custom tags spelling error, you can mark registered by makers in the field pytest.ini file, if not in the field labeled markers, will complain pytest runtime.

[Pytest]

markers =

smoke: run smoke testcase

func: run functionarity testcase

After the registration of a mark, and can be viewed by pytest --makers

Fifth, script writing

1. execution of a single test case

Create a .py file (test_001.py), for a simple functional test.

#coding=utf-8

Method #

def func(x):

    return x + 1

# Test

def test_answer():

    assert func(3) == 5

Note: The test case function names should be all lowercase, and must begin test_, such as test_answer

Change to the directory where the test files by "pytest" command to run the test

c:\python_ts>pytest

2. Perform multiple test cases

You can create multiple test cases in a test class:

#coding=utf-8

class TestClass:

    def test_one(self):

        x = "this"

        assert "h" in x

    def test_two(self):

        x = "hello"

        assert x == "hi"

Note: The test class naming convention was beginning to Test

pytest specify the file to run:

>pytest -q test_002.py

-q is quiet. It represents the output report in quiet mode. -q not output version information pytest of.

 

If there are no parameters, pytest will view the current directory and all subdirectories test file (or the beginning of test_

End _test) and run. You can also specify a list of filenames, directory names, these names.

 

Summary discovery rules

• Test files should be named test_.py or _test.py

• Test methods and functions should be named test_.

• Test class should be named Test

Result type:

The following are some common features of the test results:

• PASSED (.): The test is successful.

• FAILED (F): test failed.

• ERROR (E): error

3. Call pytest from python code

Python code executed by pytest

(1) Direct execution pytest.main () [automatically find the current directory, py file to file beginning or ending _test of test_]

(2) Set the execution parameters pytest pytest.main ([ '-. Html = / report.html', 'test_xxx.py']) [test_login.py performance report file, and generates html format]

In mode 2, pytest.main () is actually a List parameter, plug-in parameters and performance parameters, when a plurality of a plurality of parameters [] by the 'comma' is divided

For example, in C: \ There are three py file python_testscript directory

python_testscript/

├── test_001.py

 

├── test_002.py

 

└── test_003.py

Wherein test_003.py code is as follows:

import pytest

def test_main():

    assert 5 != 5

if __name__ == '__main__':

    pytest.main ()

pytest.main without parameters directly run the program, run Sublime press Ctrl + B. The results are as follows:

============================= test session starts =============================

platform win32 -- Python 3.7.1, pytest-4.0.2, py-1.7.0, pluggy-0.8.0

rootdir: C:\python_testscript, inifile:

plugins: metadata-1.7.0, html-1.19.0

collected 4 items

 

test_001.py F                                                            [ 25%]

test_002.py .F                                                            [ 75%]

test_003.py F                                                            [100%]

 

================================== FAILURES ===================================

_________________________________ test_answer _________________________________

 

    def test_answer():

>       assert func(3) == 5

E       assert 4 == 5

E        +  where 4 = func(3)

 

test_001.py:9: AssertionError

_____________________________ TestClass.test_two ______________________________

 

self = <test_002.TestClass object at 0x0000000003791A90>

 

    def test_two(self):

        x = "hello"

>       assert x == "hi"

E       AssertionError: assert 'hello' == 'hi'

E         - hello

E         + hi

 

test_002.py:11: AssertionError

__________________________________ test_main __________________________________

 

    def test_main():

>       assert 5 != 5

E       assert 5 != 5

 

test_003.py:4: AssertionError

===================== 3 failed, 1 passed in 0.22 seconds ======================

[Finished in 1.4s]

Seen from the results, main () default implementation of all the test files in the current directory files are located.

So, if we want to run a test file it? You can () is added to the main parameters:

import pytest

def test_main():

    assert 5 != 5

if __name__ == '__main__':

    pytest.main(['test_001.py'])

============================= test session starts =============================

platform win32 -- Python 3.7.1, pytest-4.0.2, py-1.7.0, pluggy-0.8.0

rootdir: C:\python_testscript, inifile:

plugins: metadata-1.7.0, html-1.19.0

collected 1 item

 

test_001.py F                                                            [100%]

 

================================== FAILURES ===================================

_________________________________ test_answer _________________________________

 

    def test_answer():

>       assert func(3) == 5

E       assert 4 == 5

E        +  where 4 = func(3)

 

test_001.py:9: AssertionError

========================== 1 failed in 0.06 seconds ===========================

[Finished in 1.2s]

4. Common Parameters

(1) Article by Example N failed stopped

pytest -x # with the first embodiment after a failure to stop running

pytest --maxfail = 2 # stops operating after the failure of two cases with

(2) specify the execution Example

pytest test_abc.py # performed in all use cases test_abc.py

pytest testing / # perform all use cases under the directory testing

pytest test_abc.py::TestClassA::test_sample # test_sample execution method test_abc.py file TestClassA test class

AR161 # executed under the current directory by tag pytest -v -m, fixture of use cases AR1610

(3) cyclic execution Example

pytest --count 30 # --count band parameter specifies the number of cycles (packages need to pytest-repeat)

(4) generate test reports

pytest --junitxml = path # Type generate test reports junit

pytest -v --html = testresult.html # Type test reports generated html

(5) cmd command-line execution of print information

pytest --capture=no

(6) other parameters

pytest -v # Output details

pytest -q # silent mode, no output python version information, etc.

The common assertion and exception handling

(1) common assertion:

assert a == b, "adding remark information assertion"

assert a! = b, "adding remark information assertion"

assert a, "adding remark information assertion"

assert not a, "adding remark information assertion"

assert "abc" in "123abc", "add remark information assertion"

assert "ef" not in "123abc", "add remark information assertion

(2) python standard exceptions

Exception Name Description

Base class for all exceptions BaseException

SystemExit interpreter requested to exit

KeyboardInterrupt User Interrupt Executing (usually enter ^ C)

General Error Exception base class

StopIteration iterator no more value

To notify the exit abnormal GeneratorExit Generator (generator) occurs

All standard exceptions built StandardError base class

All numerical errors ArithmeticError base class

FloatingPointError floating point calculation error

Exceeds the maximum limit value calculation OverflowError

ZeroDivisionError addition (or modulus) of zero (all data types)

AssertionError assertion failure

AttributeError object does not have this property

EOFError no built-in input reaches EOF marker

EnvironmentError base class operating system error

IOError input / output operations to fail

OSError operating system error

WindowsError system call fails

ImportError import module / object failed

LookupError invalid base class data queries

No sequence IndexError the index (index)

KeyError map does not have this key

MemoryError memory overflow error (for Python interpreter is not fatal)

NameError not declare / initialize objects (no attributes)

UnboundLocalError access to the local variables uninitialized

ReferenceError weak reference object (Weak reference) have been trying to access a garbage collection of

RuntimeError general run-time error

Methods NotImplementedError unfulfilled

SyntaxError Python syntax error

IndentationError indentation errors

TabError Tab and space mix

SystemError general explanation of system error

TypeError invalid type of operation

Invalid parameter passed ValueError

UnicodeError Unicode related errors

Error when decoding UnicodeDecodeError Unicode

UnicodeEncodeError Unicode encoding error

UnicodeTranslateError Unicode conversion error

Warning The base class

DeprecationWarning warnings about deprecated features

FutureWarning warnings about the future structure of the semantics have changed

OverflowWarning old warning about automatically promoted to a long integer (long) of

PendingDeprecationWarning on features will be abandoned warning

RuntimeWarning suspicious behavior (runtime behavior) run-time warning

SyntaxWarning suspicious warning grammar

UserWarning user code generated warning

(3) Exception Handling

1)try...except

a=10

b=0

try:

    c=a/b

except:

    print("error")

print("done")

2) try .... except ... else statement, when no exception occurs, else statements will be executed

a=10

b=0

try:

    c = b/ a

except:

    print("error")

else:

    print("no error")

print("done")

3) raise raises an exception

inputValue=input("please input a int data :")

if type(inputValue)!=type(1):

    raise ValueError

else:

    print(inputValue)

4) try ... finally, regardless of whether an exception occurs, before the end of the program, finally the statement will be executed

a=10

b=0

try:

    c = a/ b

finally:

    print("always excute")

finally statement can also be used with except statement

a=10

b=0

try:

    c = a/ b

except:

    print("error")

finally:

    print("always excute")

6. Regular Expressions

Must import re

1) re.match function

re.match try to match a pattern from a starting position of the string, the start position if not successful match, match () returns none.

Such as:

import re

print (re.match ( 'www', 'www.123.com') .group ()) # match in the starting position

print (re.match ( 'com', 'www.123.com')) # does not match the start position

Output:

www

None

2) re.search method

re.search first scan the entire string and returns a successful match.

Such as:

import re

print(re.search('www', 'www.123.com').group()) 

print(re.search('com', 'www.123.com').group())

Output:

www

with

re.match and re.search difference:

re.match matches only the beginning of the string, if the string does not conform to begin regular expression, the match fails, the function returns None; and re.search match the entire string until it finds a match.

3) re.findall function

Find expression in the string being matched by all the sub-string and returns a list, if no match is found, an empty list is returned.

Note: match and search is a match, findall match all.

import re

print(re.finditer(r"\d+","12a32bc43de3")) 

Output:

['12', '32', '43', '3']

4) re. Finditer function

And findall Similarly, the string is found in the positive expression matched all substrings, and returns them as an iterator.

import re

it = re.finditer(r"\d+","12a32bc43de3")

for match in it:

    print (match.group() )

Output:

12

32

43

3

Sixth, the test suite properties directory contents, for example

1. Test Suite is the characteristic of a directory name, such as testcase stored under / startup, characteristic directory __init __. Py, conftest.py and requirements.py and test script file

1) __ init__.py: empty file you can, identify the directory as a package

2) contest.py: definition script setup and teardown, the following structure:

# ------------------------------------------------- -------

# comment section

# ------------------------------------- -------------------

# Dependent module

import os,sys
sys.path.append(os.path.split(os.path.realpath(__file__))[0])
from requirements import *

@pytest.fixture(scope='package', autouse=True)
def testuite_setup_teardown():

# Preparation: all use cases executed once for each property before execution
Log.log ( '\ n ------------ testsuite setup begin -------------- ')
dta.login ()


yield

# Environmental clean-up: all execution characteristic of each embodiment performs a complete
Log.log ( '\ n ------------ testsuite teardown begin --------------' )

 

@pytest.fixture(scope='function', autouse=True)
def testcase_setup_teardown():

Preparation #: executed once before each use case run
Log.log ( '\ n------------- -------------- TestCase the begin Setup')
the yield

# Environmental clean-up: After each operation is performed once with Example
Log.log ( '\ n ------------ testcase teardown begin --------------')

3) requirements.py: store all modules need to rely on

###################### system module ##########################
Time Import
Import datetime
Import Re
Import pytest
Import SYS
Import os
from ctypes Import *

######################pytest_testlib_base模块##########################
import Log
import rpyc_client
import rpyc_server

######################本地自定义common模块##########################
sys.path.append("%s/../../common/"%(os.path.split(os.path.realpath(__file__))[0]))
sys.path.append("%s/../../common/STARTUP"%(os.path.split(os.path.realpath(__file__))[0]))
import py_base
import file_method

4) test script file:

# --------------------------------------------------------

#
# -
# Use Case Number: TESTCASE_1001
# Use Case Name: test Demo
# --------------------------------- -----------------------

from requirements import *

@pytest.fixture(scope='function', autouse=True)
def prepare_cleanup():
Log.log('\n------------prepare begin--------------')
yield
Log.log('\n------------cleanup begin--------------')

def  TESTCASE_1001(): 

       Log.log ( "Procedure 1, the device starts with the expected result 1")

       # Py_base.py documents and check_startup way to define common folder, a custom common module declaration introduced by local requirements.py throughout the project directory

       py_base.check_startup(dev_a)

       Log.log ( "expected results 1, the device starts")

Guess you like

Origin www.cnblogs.com/fyly/p/11223336.html