添加mark标记
import pytest
class TestClass ( object ) :
@pytest. mark. slow
def test_one ( self) :
x = 'this'
assert 'h' in x
def test_two ( self) :
x = 'hello'
assert hasattr ( x, 'hello' )
去除警告解决方案: Unknown pytest.mark.slow - is this a typo?
def pytest_configure ( config) :
config. addinivalue_line(
"markers" , "slow"
)
使用autos调用fixture
fixture decorator一个optional的参数是autouse, 默认设置为False。
当默认为False,就可以选择用上面方式来调用fixture标识的函数。
当设置为True时,在一个session内的所有的test都会自动调用这个fixture。
作为函数入参fixture
import pytest
@pytest. fixture( )
def run ( ) :
print ( '111111' )
def test_run ( run) :
pass
setup和teardown及参数化
@pytest. fixture( scope= 'class' )
def run ( ) :
print ( '开始' )
yield
print ( '结束' )
@pytest. fixture( )
def func ( ) :
print ( '函数开始' )
yield
print ( '函数结束' )
import pytest
@pytest. mark. usefixtures( 'run' , 'func' )
class Test_run ( ) :
def test_run ( self) :
print ( 'testrun' )
def test_run1 ( self) :
print ( 'testrun1' )
@pytest. mark. parametrize( 'a,b' , [ ( 1 , 2 ) , ( 3 , 4 ) ] )
def test_parms ( self, a, b) :
print ( a, b)
skip/skipif
@pytest. mark. skip( )
def test_skip ( ) :
print ( '跳过' )
@pytest. mark. skipif( '1>2' )
def test_skipif ( ) :
print ( '跳过' )
boo = pytest. mark. skipif( '1>2' , reason= "1>2" )
@boo
def test_skipif ( ) :
print ( '跳过' )
XFail
@pytest. mark. xfail( )
def test_function ( ) :
raise
生成报告
pytest. main( [ '--html=./1.html' , '--alluredir=./allure-results/' ] )