标签:ide 不用 simple div 无法 模块 efault sse unit
# 首先安装pytest模块
pip install -U pytest
pytest -q test_class.py # -q --quiet decrease verbosity( 显示简单结果)
pytest -x test_class.py # x 遇到错误时停止测试
pytest –maxfail=1 # 当用例错诨个数达到指定数量时,停止测试
# 首先安装pytest模块
pip install -U pytest
pytest -q test_class.py # -q --quiet decrease verbosity( 显示简单结果)
pytest -x test_class.py # x 遇到错误时停止测试
pytest –maxfail=1 # 当用例错诨个数达到指定数量时,停止测试
# 执行带某些关键字的用例
pytest -k "MyClass and not method" #(执行带有Myclass关键字的利用,但是不包括带method的那种用例)
#上面的例子将运行 TestMyClass.test_something 但不运行 TestMyClass.test_method_simple
# 执行带某些关键字的用例
pytest -k "MyClass and not method" #(执行带有Myclass关键字的利用,但是不包括带method的那种用例)
#上面的例子将运行 TestMyClass.test_something 但不运行 TestMyClass.test_method_simple
pytest test_demo.py::test_function1 # 运行某个模块下的函数
pytest test_demo2.py::Test_class::test_method # 运行某个类下的方法
pytest test_demo.py::test_function1 # 运行某个模块下的函数
pytest test_demo2.py::Test_class::test_method # 运行某个类下的方法
pytest -m slow
# 将运行用@ pytest.mark.slow 装饰器修饰的所有测试。后面章节会讲自定义标记 mark 的功能
pytest -m slow
# 将运行用@ pytest.mark.slow 装饰器修饰的所有测试。后面章节会讲自定义标记 mark 的功能
pytest --pyargs pkg.testing
# 返将导入 pkg.testing 并使用其文件系统位置来查找和运行测试
pytest --pyargs pkg.testing
# 返将导入 pkg.testing 并使用其文件系统位置来查找和运行测试
# 排序如下
# 行的优先级:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class
# 排序如下
# 行的优先级:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class
fixture的作用范围 fixture里面有个scope参数可以控制fixture的作用范围:session>module>class>function -function:每一个函数或方法都会调用 -class:每一个类调用一次,一个类中可以有多个方法 -module:每一个.py文件调用一次,该文件内又有多个function和class -session:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module |
# 1、conftest作用于类级
# ********************conftest.py******************
import pytest
@pytest.fixture(scope=‘class‘)
def login():
print(‘登录函数‘)
# ******************test_pratice_conf.py*************
import pytest
class TestOne:
def test_1(self, login):
print("test_1")
def test_2(self, login):
print("test_2")
if __name__ == ‘__main__‘:
pytest.main([‘-s‘, ‘test_pratice_conf.py‘])
# *****************************************************‘
"""
test_pratice_conf.py 登录函数
.test_1
.test_2
[100%]
"""
# 2、conftest作用于函数级
@pytest.fixture("function") # 默认就是作用于function
def log_out():
print("退出登录")
def test_case(log_out):
print(‘执行test_case‘)
# 3、conftest作用于模块机
# 1、conftest作用于类级
# ********************conftest.py******************
import pytest
fixture(scope=‘class‘) .
def login():
print(‘登录函数‘)
# ******************test_pratice_conf.py*************
import pytest
class TestOne:
def test_1(self, login):
print("test_1")
def test_2(self, login):
print("test_2")
if __name__ == ‘__main__‘:
pytest.main([‘-s‘, ‘test_pratice_conf.py‘])
# *****************************************************‘
"""
test_pratice_conf.py 登录函数
.test_1
.test_2
[100%]
"""
# 2、conftest作用于函数级
fixture("function") # 默认就是作用于function .
def log_out():
print("退出登录")
def test_case(log_out):
print(‘执行test_case‘)
# 3、conftest作用于模块机
@pytest.fixture()
def go_out():
print("前置生效")
yield
print("后置生效")
def test_yield(go_out):
print(‘test_yield‘)
"""
前置生效
.test_yield
后置生效
"""
fixture() .
def go_out():
print("前置生效")
yield
print("后置生效")
def test_yield(go_out):
print(‘test_yield‘)
"""
前置生效
.test_yield
后置生效
"""
import pytest
@pytest.fixture()
def get_user():
print(‘获取用户名‘)
username = ‘admin‘
a = username
return a
def test_demo(get_user):
assert get_user == ‘admin‘
if __name__ == ‘__main__‘:
pytest.main([‘-s‘, ‘test.py‘])
import pytest
fixture() .
def get_user():
print(‘获取用户名‘)
username = ‘admin‘
a = username
return a
def test_demo(get_user):
assert get_user == ‘admin‘
if __name__ == ‘__main__‘:
pytest.main([‘-s‘, ‘test.py‘])
import pytest
@pytest.fixture(scope=‘function‘)
def para_prep():
print("返回两个值")
username = ‘admin‘
password = ‘admin‘
return (username,password)
def test_demo(para_prep):
print("账户名是%s, 密码是%s" % (para_prep[0], [para_prep[1]]))
assert para_prep[1] == ‘admin‘
if __name__ == ‘__main__‘:
pytest.main([‘-s‘, ‘test.py‘])
import pytest
fixture(scope=‘function‘) .
def para_prep():
print("返回两个值")
username = ‘admin‘
password = ‘admin‘
return (username,password)
def test_demo(para_prep):
print("账户名是%s, 密码是%s" % (para_prep[0], [para_prep[1]]))
assert para_prep[1] == ‘admin‘
if __name__ == ‘__main__‘:
pytest.main([‘-s‘, ‘test.py‘])
import pytest
@pytest.fixture(scope="function")
def start():
print("每个类里面的方法都需要传入start")
a = "go"
return a
class TestDemo:
def test_one(self,start):
print("当前case1传入start的参数是%s" % start)
assert start == ‘go‘
def test_two(self, start):
print("当前case2传入start的参数是%s" % start)
assert start == ‘go‘
if __name__ == ‘__main__‘:
pytest.main([‘-s‘, ‘test.py‘])
import pytest
fixture(scope="function") .
def start():
print("每个类里面的方法都需要传入start")
a = "go"
return a
class TestDemo:
def test_one(self,start):
print("当前case1传入start的参数是%s" % start)
assert start == ‘go‘
def test_two(self, start):
print("当前case2传入start的参数是%s" % start)
assert start == ‘go‘
if __name__ == ‘__main__‘:
pytest.main([‘-s‘, ‘test.py‘])
import pytest
@pytest.fixture(scope="function")
def start():
print("每个类里面的方法都需要传入start")
@pytest.mark.usefixtures(‘start‘)
class TestDemo:
def test_one(self):
print("当前case1传入start的参数是%s")
def test_two(self):
print("当前case2传入start的参数是%s")
if __name__ == ‘__main__‘:
pytest.main([‘-s‘, ‘test.py‘])
import pytest
fixture(scope="function") .
def start():
print("每个类里面的方法都需要传入start")
mark.usefixtures(‘start‘) .
class TestDemo:
def test_one(self):
print("当前case1传入start的参数是%s")
def test_two(self):
print("当前case2传入start的参数是%s")
if __name__ == ‘__main__‘:
pytest.main([‘-s‘, ‘test.py‘])
import pytest
@pytest.fixture(scope=‘module‘, autouse=True)
def mo_kuai():
print("这个是一个模块前置方法,模块中只调用一次")
@pytest.fixture(scope=‘function‘, autouse=True)
def func():
print("这个是一个函数前置,每个函数自动调用")
def test_demo1():
print("test case one is doing")
class TestCase:
def test_me1(self):
print("这是一个类中的普通方法,test_me1")
def test_me2(self):
print("这是一个类中的普通方法,test_me2")
if __name__ == ‘__main__‘:
pytest.main([‘-s‘, ‘test.py‘])
import pytest
fixture(scope=‘module‘, autouse=True) .
def mo_kuai():
print("这个是一个模块前置方法,模块中只调用一次")
fixture(scope=‘function‘, autouse=True) .
def func():
print("这个是一个函数前置,每个函数自动调用")
def test_demo1():
print("test case one is doing")
class TestCase:
def test_me1(self):
print("这是一个类中的普通方法,test_me1")
def test_me2(self):
print("这是一个类中的普通方法,test_me2")
if __name__ == ‘__main__‘:
pytest.main([‘-s‘, ‘test.py‘])
标签:ide 不用 simple div 无法 模块 efault sse unit
原文地址:https://www.cnblogs.com/dadaizi/p/11964769.html