栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytest特性记录:运行指定测试用例

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytest特性记录:运行指定测试用例

关键字:-k

pytest运行参数 -k用于运行指定的测试用例,比如在某一个py文件中,进行脚本调试,此时只想运行某一个用例,则可以使用该参数。举例如下:

import pytest


class TestPytestFeatures(object):
    def test_case_login(self):
        num = 1
        assert num == 1

    def test_case_logout(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, list)

    def test_case_shopping(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, list)


if __name__ == "__main__":
    pytest.main(["-k shop", "test_case_pytest_features.py"])

"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:icp_capphelp
plugins: allure-pytest-2.9.43
collected 3 items / 2 deselected / 1 selected

test_case_pytest_features.py .                                           [100%]

======================= 1 passed, 2 deselected in 0.36s =======================

"""

此时还可以通过关键字 or 指定多个脚本进行运行,举例如下:

import pytest


class TestPytestFeatures(object):
    def test_case_login(self):
        num = 1
        assert num == 1

    def test_case_logout(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, list)

    def test_case_shopping(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, list)

    def test_case_walk(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, list)


if __name__ == "__main__":
    pytest.main(["-k shop or out or walk", "test_case_pytest_features.py"])

"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:icp_capphelp
plugins: allure-pytest-2.9.43
collected 4 items / 1 deselected / 3 selected

test_case_pytest_features.py ...                                         [100%]

============================== warnings summary ===============================
test_case_pytest_features.py:32
  E:icp_capphelptest_case_pytest_features.py:32: DeprecationWarning: invalid escape sequence i


-- Docs: https://docs.pytest.org/en/stable/warnings.html
================= 3 passed, 1 deselected, 1 warning in 0.09s ==================

"""

-k 与 not 和and配合使用,可以在执行用例前,排除某些用例

import pytest


class TestPytestFeatures(object):
    def test_case_login(self):
        num = 1
        assert num == 1

    def test_case_logout(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, list)

    def test_case_shopping(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, list)

    def test_case_walk(self):
        tmp = ["a", "b"]
        assert isinstance(tmp, list)


if __name__ == "__main__":
    pytest.main(["-k not shop and not out and not walk", "test_case_pytest_features.py"])

"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:icp_capphelp
plugins: allure-pytest-2.9.43
collected 4 items / 3 deselected / 1 selected

test_case_pytest_features.py .                                           [100%]

============================== warnings summary ===============================
test_case_pytest_features.py:32
  E:icp_capphelptest_case_pytest_features.py:32: DeprecationWarning: invalid escape sequence i


-- Docs: https://docs.pytest.org/en/stable/warnings.html
================= 1 passed, 3 deselected, 1 warning in 0.06s ==================

"""

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/461548.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号