import pytest
def test_1():
assert 1 == 2, '不相等'
print("这是1个测试方法")
def test_3():
print("这是2个测试方法")
def t_3():
print("这是3个测试方法")
# 默认只执行 test_开头的函数,同一个模块,优先执行test_开头函数,再执行测试类
if __name__ == 'main':
"""1、CMD模式:在当前目录,输入pytest 即可"""
# pytest[option][file_or_dir][file_or_dir]... # 中括号是可选项
# pytest[参数操作][文件或文件夹]
# pytest.main(["-x", "test_01_first.py"]) 执行到错误会停止
# pytest.main(["-s", "test_01_first.py"]) 执行全部
"""2.通过python终端: python -m pytest """
"""3. 代码种执行:pytest.main('-q',__file__) ,其中 -q 是quite缩写,表示静默执行,__file__是当前文件名"""
pytest.main(["-s", "test_01_first.py"])
pytest.main('-q', __file__)
# 控制台 pytest -s 输出控制台信息
# pytest -q 静默执行,不显示详细信息
# pytest -x 执行失败终止
# pytest -q -rA 静默执行,不显示详细信息,结束后打印 全部的简短的报告
# pytest -q -rf 静默执行,不显示详细信息,结束后打印 失败的断言
# pytest -q -rE 静默执行,不显示详细信息,结束后打印 出错的断言
# pytest -q -rs 静默执行,不显示详细信息,结束后打印 跳过执行的
# pytest -q -rp 静默执行,不显示详细信息,结束后打印 成功执行的
# pytest -q -rP 静默执行,不显示详细信息,结束后打印 成功执行的,并输出print
# pytest -q -ra 静默执行,不显示详细信息,结束后打印 非成功的
# pytest -q -rfs 静默执行,不显示详细信息,结束后打印 失败的断言
总结:
1.测试执行成功,默认通过,且可以用断言判断 assert
2.pytest 一般收集以 test_ 开头 的测试函数这类用例,其他不会被统计到执行;
3.手动在 pycharm种按模块执行时,可以添加 以下 方便执行(直接选择文件也可以执行)
pytest.main(["-s", "当前模块名"]) pytest.main('-q', __file__)