测试人社区

pytest_dependency插件使用时,所依赖的用例结果正常时,该用例依然skipped,不知道是哪的问题

被依赖的用例加上

@pytest.mark.dependency(name="test_add")
@pytest.mark.dependency(name="test_multi")

亲,你这个明显不是老师讲的点。。。。。。
@pytest.mark.dependency(depends=[“test_a”])

…你不对被依赖用例进行标记,如何引用依赖呢

被依赖的测试用例默认是要加上@pytest.mark.dependency() 但是因为你被依赖用例加了参数化,因为参数化默认会导致用例名称变化(会拼接上数据以免名称重复不好分辨),所以用了参数化的被依赖用例一般是用别名去标识

就像他回复的一样,这样才能保证你的依赖找得到被依赖的用例执行情况

我也遇到了这个问题能帮忙看一下吗

from src.pages.login_page import LoginPage
from src.utils.log import Logger
import pytest
from selenium import webdriver


class TestLogin:
    def setup_class(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()

        self.log_handle = Logger(logger_name='login', out_dir='E:\\share_content\\selenium\\project\\testcases').get_logger()
        self.login_page = LoginPage(self.driver, log_handle=self.log_handle)

    # def teardown_class(self):
    #     self.driver.quit()

    @pytest.mark.dependency(name='user_login')
    def test_login_success(self):
        self.login_page.login('ezviz', 'ezviz')
from src.pages.ucenter_page import UserCenterPage
from src.utils.log import Logger
from testcases.test_login import TestLogin

import pytest


class TestUserCenter:
    def setup_class(self):
        self.log_handle = Logger(logger_name='user_center', out_dir='E:\\share_content\\selenium\\project\\testcases').get_logger()
        # self.user_center_page = UserCenterPage(self.driver, log_handle=self.log_handle)
        self.login = TestLogin()
        # self.login.test_login_success()
        # self.driver = self.login.driver
        # self.user_page = UserCenterPage(self.driver, log_handle=self.log_handle)

    @pytest.mark.dependency(depends=['user_login'], scope='module')
    def test_setting_userinfo(self):
        print(123)
        # self.user_page.avatar_setting('E:\\share_content\\selenium\\project\\testcases\\demo.jpeg')

执行的时候会报

============================= test session starts =============================
platform win32 -- Python 3.9.0, pytest-6.1.2, py-1.9.0, pluggy-0.13.1 -- E:\share_content\selenium\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\share_content\selenium\project\testcases
plugins: allure-pytest-2.8.19, dependency-0.5.1
collecting ... collected 1 item

test_seting_avatar.py::TestUserCenter::test_setting_userinfo SKIPPED     [100%]
Skipped: test_setting_userinfo depends on user_login


============================= 1 skipped in 0.14s ==============================

貌似和执行用例的路径有关系 ,

import pytest
import os

if __name__ == '__main__':
    pytest.main(['testcases/test_seting_avatar.py', '--alluredir', './report'])
    os.system('allure generate ./report -o ./html --clean')

这样就可以执行,直接在用例自身上使用pycharm去点击执行就会skip,应该是依赖找不到的原因 奇怪