Never mix unittest and pytest tests

This commit is contained in:
Pedro Algarvio 2020-09-02 09:15:36 +01:00
parent 43b23f7744
commit 569a99cf46
No known key found for this signature in database
GPG key ID: BB36BF6584A298FF
2 changed files with 29 additions and 46 deletions

View file

@ -17,6 +17,7 @@ import pprint
import re
import sys
from functools import partial, wraps
from unittest import TestCase # pylint: disable=blacklisted-module
import _pytest.logging
import _pytest.skipping
@ -41,6 +42,7 @@ from tests.support.saltfactories_compat import LogServer
from tests.support.sminion import check_required_sminion_attributes, create_sminion
TESTS_DIR = pathlib.Path(__file__).resolve().parent
PYTESTS_DIR = TESTS_DIR / "pytests"
CODE_DIR = TESTS_DIR.parent
# Change to code checkout directory
@ -328,6 +330,28 @@ def pytest_report_header():
return "max open files; soft: {}; hard: {}".format(soft, hard)
def pytest_itemcollected(item):
"""We just collected a test item."""
try:
pathlib.Path(item.fspath.strpath).resolve().relative_to(PYTESTS_DIR)
# Test is under tests/pytests
if item.cls and issubclass(item.cls, TestCase):
pytest.fail(
"The tests under {0!r} MUST NOT use unittest's TestCase class or a subclass of it. "
"Please move {1!r} outside of {0!r}".format(
str(PYTESTS_DIR.relative_to(CODE_DIR)), item.nodeid
)
)
except ValueError:
# Test is not under tests/pytests
if not item.cls or (item.cls and not issubclass(item.cls, TestCase)):
pytest.fail(
"The test {!r} appears to be written for pytest but it's not under {!r}. Please move it there.".format(
item.nodeid, str(PYTESTS_DIR.relative_to(CODE_DIR)), pytrace=False
)
)
@pytest.hookimpl(hookwrapper=True, trylast=True)
def pytest_collection_modifyitems(config, items):
"""
@ -549,7 +573,7 @@ def pytest_runtest_setup(item):
if salt.utils.platform.is_windows():
unit_tests_paths = (
str(TESTS_DIR / "unit"),
str(TESTS_DIR / "pytests" / "unit"),
str(PYTESTS_DIR / "unit"),
)
if not str(pathlib.Path(item.fspath).resolve()).startswith(unit_tests_paths):
# Unit tests are whitelisted on windows by default, so, we're only
@ -657,10 +681,10 @@ def from_filenames_collection_modifyitems(config, items):
(TESTS_DIR / "integration").relative_to(CODE_DIR),
(TESTS_DIR / "multimaster").relative_to(CODE_DIR),
(TESTS_DIR / "unit").relative_to(CODE_DIR),
(TESTS_DIR / "pytests" / "e2e").relative_to(CODE_DIR),
(TESTS_DIR / "pytests" / "functional").relative_to(CODE_DIR),
(TESTS_DIR / "pytests" / "integration").relative_to(CODE_DIR),
(TESTS_DIR / "pytests" / "unit").relative_to(CODE_DIR),
(PYTESTS_DIR / "e2e").relative_to(CODE_DIR),
(PYTESTS_DIR / "functional").relative_to(CODE_DIR),
(PYTESTS_DIR / "integration").relative_to(CODE_DIR),
(PYTESTS_DIR / "unit").relative_to(CODE_DIR),
)
test_module_paths = set()

View file

@ -1,41 +0,0 @@
# -*- coding: utf-8 -*-
"""
tests.pytests.conftest
~~~~~~~~~~~~~~~~~~~~~~
"""
import pathlib
import pytest
from tests.support.runtests import RUNTIME_VARS
from tests.support.unit import TestCase
PYTESTS_SUITE_PATH = pathlib.Path(__file__).parent
@pytest.hookimpl(hookwrapper=True, trylast=True)
def pytest_collection_modifyitems(config, items):
"""
called after collection has been performed, may filter or re-order
the items in-place.
:param _pytest.main.Session session: the pytest session object
:param _pytest.config.Config config: pytest config object
:param List[_pytest.nodes.Item] items: list of item objects
"""
# Let PyTest or other plugins handle the initial collection
yield
# Check each collected item that's under this package to ensure that none is using TestCase as the base class
for item in items:
if not str(item.fspath).startswith(str(PYTESTS_SUITE_PATH)):
continue
if not item.cls:
# The test item is not part of a class
continue
if issubclass(item.cls, TestCase):
raise RuntimeError(
"The tests under {} MUST NOT use unittest's TestCase class or a subclass of it.".format(
pathlib.Path(str(item.fspath)).relative_to(RUNTIME_VARS.CODE_DIR)
)
)