python接口自动化

运行方式详解:

1.命令行模式

输入pytest运行即可
参数:
-vs -v输出更详细的信息,-s表示输出调试信息
-n 多线程运行测试用例
--reruns 失败用例重跑
raise Exception("巴拉巴拉巴拉")
--html 生成测试报告
--html=/.reports.report.html

2.主函数模式

pytest.main()

3.基于pytest.ini的配置文件运行

[pytest]
adopts = -vs
test-paths = ./testcases
python_files = test.py
python_classes = Test

python_functions = test_*
markers =
smoke:冒烟测试

@pytest.mark.smoke

增加标记后可以通过如下方式只执行带标记的用例

adopts = -vs -m smoke

测试用例的前后置、固件、夹具

    def setup(self):
        print("在每个用例之前执行一次:初始化日志对象,初始化数据库连接")

    def teardown(self):
        print("在每个用例之后执行一次:关闭日志对象,关闭数据库连接")

    def setup_class(self):
        print("在每个类之前执行")

    def teardown_class(self):
        print("在每个类之后执行")

@pytest.fixture装饰器可以实现部分用例前后置。
@pytest.fixture(scope="", params="", autouse="", ids="", name="")
scope:作用域
function,class,module,package/session

@pytest.fixture(scope="function")
def exe_sql():
    print("用例之前")
    yield
    print("用例之后")


@pytest.fixture(scope="class", autouse=True)
def exe_sql():
    print("类之前")
    yield
    print("类之后")

如果scope=function,那么可以在用例的参数后面单独调用。
如果scope="class",那么可以在类上面通过参数后面单独调用。

@pytest.fixture(scope="class")
def exe_sql():
    print("类之前")
    yield
    print("类之后")


@pytest.mark.usefixtures("exe_sql")
class TestQian:
    def test_jing(self):
        print("测试")

你可能感兴趣的:(python接口自动化)