一、 case运行方式
1、terminal(终端)中输入包含case的文件名称
pytest test_one.py
运行文件夹,只执行以test开头的.py文件
pytest testrule/
2、在系统设置–设置运行方式为pytest
preferences–tools–python integrated tools
testing选择pytest
二、文件命名规范
1、.py测试文件必须以“test_”开头(或“test”结尾)
2、测试方法必须以“test”开头
3、测试类必须以Test开头,而且不能有init方法
三、测试用例的执行顺序
1、默认执行顺序
2、使用pytest-ordering自定义顺序
'''1.登录 2.查找商品 3.下单 4.支付'''
import pytest
@pytest.mark.run(order=1)
def test_login():
print('login...')
@pytest.mark.run(order=4)
def test_pay():
print('pay...')
@pytest.mark.run(order=2)
def test_search():
print('serach...')
@pytest.mark.run(order=3)
def test_order():
print('order...')
四、常用断言类型
1、等于:==
2、不等于:!=
3、大于:>
4、小于:<
5、属于:in
6、不属于:not in
7、大于等于:>=
8、小于等于:<=
9、是:is
10、不是:is not
def test_assert():
assert 1 != 2
assert 2 > 1
assert 1 < 2
assert 1 >= 1
assert 1 <= 1
assert 'a' in 'abc'
assert 'a' not in 'bc'
assert True is True
assert False is not True
五、pytest配置项
在主目录创建文件pytest.ini
[pytest]
# ./表示当前目录,testrule为测试工作中指定的测试文件夹
testpaths = ./testrule
六、pytest运行参数
1、pytest -m 执行特定的测试用例
'''
@Author : 测试工程师Selina
@FileName : pytest.ini
@Description:
'''
[pytest]
testpaths = ./testcase
markers=
p0:高优先级
test:测试环境
pro:生产环境
(1)给方法加标签
'''
@Author : 测试工程师Selina
@FileName : test_rule.py
@Description:
'''
import pytest
@pytest.mark.p0
def test_one():
expect = 1
actual = 2
assert expect == actual
@pytest.mark.test
def test_two():
expect = 1
actual = 1
assert expect == actual
运行代码:
# 在终端运行代码
pytest -m 'p0'
pytest -m 'test'
(2)给类加标签
'''
@Author : 测试工程师Selina
@FileName : test_rule.py
@Description:
'''
import pytest
@pytest.mark.pro
class TestThree:
def test_one(self):
expect = 1
actual = 2
assert expect == actual
def test_two(self):
expect = 1
actual = 1
assert expect == actual
运行代码:
# 在终端运行代码
pytest -m 'pro'
2、pytest -k 执行用例包括‘关键字’的用例
执行的时候,如果文件名匹配,文件名下面的所有测试方法都会执行
如果文件名不匹配,测试方法的名称匹配,则会执行测试方法
3、 pytest -q 说明:简化控制台的输出
4、pytest -v 可以输出用例更加详细的执行信息
5、pytest -s 输出用例中的调试信息
6、pytest使用ini配置指定运行参数