pytest笔记:PytestUnknownMarkWarning、fixture XXX not found的解决方法

本文为博主原创,未经许可严禁转载。
本文链接:https://blog.csdn.net/zyooooxie/article/details/113977322

The pytest framework makes it easy to write small tests;所以我就偏爱它了;但最近遇到了些问题,解决后,做个分享。

个人博客:https://blog.csdn.net/zyooooxie

2种 PytestUnknownMarkWarning

我用pytest在执行用例后,发现有些warnings(PytestUnknownMarkWarning),虽然不影响用例的执行,但是看着总不舒服,想着咋解决。

我将 我实际遇到的PytestUnknownMarkWarning 分为2种,以warnings文件来区分。

allure-pytest的PytestUnknownMarkWarning

若我没记错, 用例如下


    @allure.feature('f1-模块')
    @allure.story('暂不使用-功能')
    @allure.title('用例1')
    def test_11(self, other):
        print('执行11')
        print(other)
        allure.dynamic.description("动态description")
        print('allure_test')


在这里插入图片描述

最初遇到此问题,我就按照网上的教程去改动,丝毫效果都没有。
我就留意到:我这儿是allure-pytest库的helper.py文件在警告,
就是说 这个库有问题???
试着去升级下,
再执行,已解决。

在这里插入图片描述

执行结果:

在这里插入图片描述

测试用例.py 的PytestUnknownMarkWarning

File:conftest.py

import pytest


@pytest.fixture(scope='function')
def other():
    print('前置')
    abc = 1
    yield abc

    print('后置')

File:test_pra33.py


import pytest
from _pytest.warning_types import PytestUnknownMarkWarning


class TestPractice(object):

    @pytest.mark.last
    def test_1(self, other):
        print('执行1')
        print(other)
        assert 1 == 1

    @pytest.mark.first
    def test_2(self, other):
        print(other)
        print('执行2')
        assert 5 > 6

    def test_3(self, other):
        print('执行3')
        assert 8 < 9
        print(other)

    @pytest.mark.zyooooxie
    def test_4(self, other):
        print('执行4')
        assert 8 > 10
        print(other)

    @pytest.mark.zy
    def test_5(self, other):
        print('执行5')
        print(other)
        assert 5 == 4

    @pytest.mark.smoke
    def test_6(self, other):
        print('执行6')
        print(other)
        print('冒烟test')


if __name__ == '__main__':
    pytest.main(['-s', 'test_pra33.py'])

执行结果如下: 【看到5个PytestUnknownMarkWarning】

在这里插入图片描述

那 PytestUnknownMarkWarning 到底是什么东西?
翻看 _pytest.warning_types

Warning emitted on use of unknown markers. 【未知markers的Warning】

在这里插入图片描述

看下 注释中的 https://docs.pytest.org/en/latest/mark.html ; 找到了些解决方法:

解决方法:Registering marks

在这里插入图片描述

pyproject.toml这方法 我本地没试成功 ;【升级pytest 6.0后,HttpRunner又限制了pytest<6.0.0,>=5.4.2,很尴尬啊】

在这里插入图片描述

我这儿 就展示另2种 :pytest.ini中设置markers、使用pytest_configure() ;

File:conftest.py


def pytest_configure(config):
    config.addinivalue_line("markers", "last: Last run.")

在pytest.ini文件:

在这里插入图片描述

再次执行 结果:

只有3个PytestUnknownMarkWarning 【另外2个已经注册】

在这里插入图片描述

倘若很多mark呢

File:conftest.py

请留意 一定不能是 pytest_configure_all() 【Initialization hooks没有这个】

https://docs.pytest.org/en/latest/reference.html#initialization-hooks

在这里插入图片描述

def pytest_configure(config):
    mark = ['zy: name', 'zyooooxie']
    for i in mark:
        config.addinivalue_line("markers", i)

在pytest.ini文件:

在这里插入图片描述

再次执行,结果:
只有1个,其他4个已经注册;

在这里插入图片描述

解决方法 :using the filterwarnings ini option

执行结果下方 Docs: https://docs.pytest.org/en/latest/warnings.html 也给出几种方法,看下

A. using the filterwarnings ini option

在这里插入图片描述

在这里插入图片描述

B. use the --disable-warnings command-line option

在这里插入图片描述

在这里插入图片描述

C. disabling warning capture entirely

在这里插入图片描述

在这里插入图片描述

上说这些方法 都能搞定PytestUnknownMarkWarning;
我更推荐的是 Registering marks;

fixture ‘xxx‘ not found

某project:

在这里插入图片描述

File:conftest.py

import pytest


@pytest.fixture(scope='function')
def driver():
    print('前')
    abc = 123
    yield abc
    print('后')


File:test_34.py

import pytest


class TestPractice(object):


    def test_1(self, driver):
        print(driver)
        assert 1 == 2



if __name__ == '__main__':
    pytest.main(['-s', 'test_34.py'])


看下执行结果:

在这里插入图片描述

一看,这报错fixture driver not found,我感觉 十有八九就是写法有误啊;
在反复检查后,确认我写得没毛病啊,
就去网上找答案了。

简单翻看,咋感觉 人家的和我的完全不搭呀。。。
咋整???

回到run页面,rootdir: D:, inifile: pytest.ini 很奇怪啊;

看下官方文档 https://docs.pytest.org/en/stable/customize.html

在这里插入图片描述

我的判断是 初始化时 rootdir出错;

去我本地的d盘看下;确实有 pytest.ini ;所以就 将其重命名。

在这里插入图片描述

再次执行,已解决

在这里插入图片描述

交流技术 欢迎+QQ 153132336 zy
个人博客 https://blog.csdn.net/zyooooxie

猜你喜欢

转载自blog.csdn.net/zyooooxie/article/details/113977322