pytest-dependency is not working in my test

2k Views Asked by At

There are 2 files, the code in the first one is:

import pytest

class TestXdist2():
    @pytest.mark.dependency(name="aa")
    def test_t1(self):
        print("\ntest_1")
        assert True

the code in the second file is:

import pytest
import sys, os
sys.path.append(os.getcwd())
from testcases.test_xdist_2 import TestXdist2

class TestXdist1():

    def setup_class(self):
        self.x = TestXdist2()

    @pytest.mark.dependency(depends=["aa"], scope="module")
    def test_t2(self):
        print("\ntest_t2")
        assert 1==1

if __name__ == "__main__":
    pytest.main(["-s", "-v", f"{os.path.abspath('testcases')}/test_xdist_1.py"])

when I run the senond file, I thought test case "test_t1" should be ran firstly, then "test_t2" ran secondly, but the result is like this, "test_t2" is skipped, I don'y know why,

PS D:\gitProjects\selenium_pytest_demo> & D:/Python38/python.exe d:/gitProjects/selenium_pytest_demo/testcases/test_xdist_1.py
Test session starts (platform: win32, Python 3.8.7, pytest 6.2.2, pytest-sugar 0.9.4)
cachedir: .pytest_cache
metadata: {'Python': '3.8.7rc1', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'pytest': '6.2.2', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.8.35', 'dependency': '0.5.1', 'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'rerunfailures': '9.1.1', 'sugar': '0.9.4', 'xdist': '2.2.1'}, 'JAVA_HOME': 'D:\\Java\\jdk-15.0.1'}
rootdir: D:\gitProjects\selenium_pytest_demo, configfile: pytest.ini
plugins: allure-pytest-2.8.35, dependency-0.5.1, forked-1.3.0, html-3.1.1, metadata-1.11.0, rerunfailures-9.1.1, sugar-0.9.4, xdist-2.2.1
collecting ... 
 testcases\test_xdist_1.py::TestXdist1.test_t2 s                                                                                                        50% █████     

test_1

 testcases\test_xdist_2.py::TestXdist2.test_t1 ✓                                                                                                       100% ██████████

Results (0.04s):
       1 passed
       1 skipped
1

There are 1 best solutions below

1
On

This is the expected behavior - pytest-dependency does not order testcases, it only skips testcases if the testcase they depend on is skipped or failed. There exists a PR that would change that, but is not merged

Until that, you can use pytest-order. If you just want the ordering, you can use relative markers. If you also want to skip tests if the test they depend on failed, you can use pytest-dependency as before, but use the pytest-order option --order-dependencies to order the tests additionally.

Disclaimer:
I'm the author of pytest-order (which is a fork of pytest-ordering).