Skip to content

Commit

Permalink
Feature/323 import file mismatch (#324)
Browse files Browse the repository at this point in the history
* Latest version of `nox_utils`

* Fixed import errors with latest pytest

* changelog and noxfile for pytest 8

* Fixes part of #321 by solving issues related to the new fixture event_loop_policy in our tests

* Fixed tests. updated changelog

* Fixed pytest-asyncio 0.23 issue

---------

Co-authored-by: Sylvain MARIE <[email protected]>
  • Loading branch information
smarie and Sylvain MARIE authored Jan 11, 2024
1 parent 998429f commit 59ab8d2
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 13 deletions.
6 changes: 4 additions & 2 deletions ci_tools/nox_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import re
from shlex import split
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -130,7 +131,7 @@ def run2(self, command: Union[Iterable[str], str], logfile: Union[bool, str, Pat
:return:
"""
if isinstance(command, str):
command = command.split(" ")
command = split(command)

self.run(*command, logfile=logfile, **kwargs)

Expand Down Expand Up @@ -632,7 +633,8 @@ def _f_wrapper(**kwargs):
except KeyError:
# Skip this session, it is a dummy one
nox_logger.warning(
"Skipping configuration, this is not supported in python version %r" % session.python
"Skipping configuration, %r is not meant to be executed in this now session for python version %r" %
(grid_param if has_parameter else "this", session.python)
)
return

Expand Down
9 changes: 8 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

### 3.8.2 (in progress) - bugfixes
### 3.8.2 - bugfixes and project improvements

- Corrected API documentation (and comments) for the second file-name
pattern for `AUTO`-cases lookup (`cases_<name>.py` instead of
Expand All @@ -10,6 +10,13 @@
Fixes [#309](https://github.com/smarie/python-pytest-cases/issues/309). PR
[#320](https://github.com/smarie/python-pytest-cases/pull/320) by
[@michele-riva](https://github.com/michele-riva).
- Improved error message in case of cases loading error in `@parametrize_with_cases` when the `cases` argument
is a string refering to a relative or absolute module name. Fixed `import file mismatch` with
pytest 8 when executing our own tests.
Fixes [#323](https://github.com/smarie/python-pytest-cases/issues/323).
- Fixed failing tests in our builds due to the `event_loop_policy` fixture that appeared in `pytest-asyncio` `0.23`.
Fixes part of
[#321](https://github.com/smarie/python-pytest-cases/issues/321).

### 3.8.1 - bugfixes

Expand Down
7 changes: 7 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@


ENVS = {
# python 3.12
(PY312, "pytest-latest"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": ""}},
(PY312, "pytest7.x"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": "<8"}},
# python 3.11
(PY311, "pytest-latest"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": ""}},
# python 3.10
(PY310, "pytest-latest"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": ""}},
# python 3.9 - put first to detect easy issues faster.
(PY39, "pytest-latest"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": ""}},
(PY39, "pytest7.x"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": "<8"}},
(PY39, "pytest6.x"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": "<7"}},
# python 3.8
(PY38, "pytest4.x"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": "<5"}},
(PY38, "pytest5.x"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": "<6", "pytest-asyncio": DONT_INSTALL}},
(PY38, "pytest6.x"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": "<7"}},
(PY38, "pytest7.x"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": "<8"}},
(PY38, "pytest-latest"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": ""}},
# python 2.7
(PY27, "pytest3.x"): {"coverage": False, "pkg_specs": {"pip": ">10", "pytest": "<4", "pytest-asyncio": DONT_INSTALL}},
Expand All @@ -48,6 +54,7 @@
(PY37, "pytest4.x"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": "<5"}},
(PY37, "pytest5.x"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": "<6", "pytest-asyncio": DONT_INSTALL}},
(PY37, "pytest6.x"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": "<7"}},
(PY37, "pytest7.x"): {"coverage": False, "pkg_specs": {"pip": ">19", "pytest": "<8"}},
# IMPORTANT: this should be last so that the folder docs/reports is not deleted afterwards
(PY37, "pytest-latest"): {"coverage": True, "pkg_specs": {"pip": ">19", "pytest": ""}}
}
Expand Down
8 changes: 7 additions & 1 deletion src/pytest_cases/case_parametrizer_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,13 @@ def extract_cases_from_module(module, # type: ModuleRe
"""
# optionally import module if passed as module name string
if isinstance(module, string_types):
module = import_module(module, package=package_name)
try:
module = import_module(module, package=package_name)
except ModuleNotFoundError as e:
raise ModuleNotFoundError(
"Error loading cases from module. `import_module(%r, package=%r)` raised an error: %r"
% (module, package_name, e)
)

return _extract_cases_from_module_or_class(module=module, _case_param_factory=_case_param_factory,
case_fun_prefix=case_fun_prefix)
Expand Down
1 change: 1 addition & 0 deletions src/pytest_cases/pep492.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ async def wrapped_fixture_func(*_args, **_kwargs):

return wrapped_fixture_func


def _parametrize_plus_decorate_coroutine_pep492(
test_func,
new_sig,
Expand Down
Empty file.
Empty file.
Empty file.
3 changes: 2 additions & 1 deletion tests/cases/issues/issue_311/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@


@fixture(scope='session')
@parametrize_with_cases('arg', cases='cases', scope='session')
@parametrize_with_cases('arg', cases='.cases', scope='session')
def scope_mismatch(arg):
return [arg]


session_scoped = scope_mismatch
Empty file.
2 changes: 1 addition & 1 deletion tests/cases/issues/issue_311/test_issue_311/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@


@fixture(scope='class')
@parametrize_with_cases('arg', cases='cases', scope='class')
@parametrize_with_cases('arg', cases='..cases', scope='class')
def class_scoped(arg):
return [arg]
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@


@fixture
@parametrize_with_cases('arg', cases='cases')
@parametrize_with_cases('arg', cases='..cases')
def function_scoped(arg):
return [arg]


# This tests would fail with a ScopeMismatch
# during collection before #317
def test_scope_mismatch_collection(scope_mismatch):
assert scope_mismatch == [1]


def test_scopes(session_scoped, function_scoped, class_scoped):
session_scoped.append(2)
function_scoped.append(2)
Expand All @@ -19,6 +21,7 @@ def test_scopes(session_scoped, function_scoped, class_scoped):
assert function_scoped == [1, 2]
assert class_scoped == [1, 2]


def test_scopes_again(session_scoped, function_scoped, class_scoped):
session_scoped.append(3)
function_scoped.append(3)
Expand Down
17 changes: 16 additions & 1 deletion tests/pytest_extension/doc/test_doc_fixture_graph_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
# + All contributors to <https://github.com/smarie/python-pytest-cases>
#
# License: 3-clause BSD, <https://github.com/smarie/python-pytest-cases/blob/master/LICENSE>
from packaging.version import Version
from pytest_cases import fixture, parametrize, fixture_union, fixture_ref


try:
import pytest_asyncio
except ImportError:
PYTEST_ASYNCIO_FIXTURE = False
else:
PYTEST_ASYNCIO_FIXTURE = Version(pytest_asyncio.__version__) >= Version('0.23.0')


@fixture(autouse=True)
@parametrize(ie=[-1, 1])
def e(ie):
Expand Down Expand Up @@ -58,7 +67,7 @@ def test_1(u, request):
def test_closure():
# make sure that the closure tree looks good
global super_closure
assert str(super_closure) == """SuperClosure with 3 alternative closures:
ref_str = """SuperClosure with 3 alternative closures:
- ['environment', 'e', 'request', 'u', 'a', 'c', 'd'] (filters: u=u[0]=a)
- ['environment', 'e', 'request', 'u', 'b', 'b_ub', 'a', 'c', 'd'] (filters: u=u[1]=b, b_ub=b_ub[0]=a)
- ['environment', 'e', 'request', 'u', 'b', 'b_ub', 'c'] (filters: u=u[1]=b, b_ub=b_ub[1]=c)
Expand All @@ -71,3 +80,9 @@ def test_closure():
- (a,c,d)
- (c)
"""

if PYTEST_ASYNCIO_FIXTURE:
ref_str = ref_str.replace("(environment,", "(event_loop_policy,environment,")
ref_str = ref_str.replace("['environment',", "['event_loop_policy', 'environment',")

assert str(super_closure) == ref_str
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
#
# License: 3-clause BSD, <https://github.com/smarie/python-pytest-cases/blob/master/LICENSE>
import warnings

from copy import copy
from packaging.version import Version

import pytest

from pytest_cases.plugin import SuperClosure
from pytest_cases import fixture, fixture_union


try:
import pytest_asyncio
except ImportError:
PYTEST_ASYNCIO_FIXTURE = False
else:
PYTEST_ASYNCIO_FIXTURE = Version(pytest_asyncio.__version__) >= Version('0.23.0')


@fixture(autouse=True)
def a():
return
Expand Down Expand Up @@ -42,8 +50,10 @@ def test_super_closure_edits2():
global super_closure
assert isinstance(super_closure, SuperClosure)
super_closure = copy(super_closure)
assert len(super_closure) == 4
reflist = ['environment', 'a', 'request', 'b']
if PYTEST_ASYNCIO_FIXTURE:
reflist = ['event_loop_policy'] + reflist
assert len(super_closure) == len(reflist)
assert list(super_closure) == reflist
assert super_closure[:] == reflist[:]
assert super_closure[1] == reflist[1]
Expand All @@ -54,9 +64,9 @@ def test_super_closure_edits2():
super_closure[1] = reflist[1]
super_closure[::2] = reflist[::2]
with pytest.warns(UserWarning):
super_closure[2:] = ['b', 'request']
super_closure[2+PYTEST_ASYNCIO_FIXTURE:] = ['b', 'request']
# the above operation is allowed but does nothing and a warning is issued.
assert super_closure[2:] == ['request', 'b']
assert super_closure[2+PYTEST_ASYNCIO_FIXTURE:] == ['request', 'b']

# removing now works
super_closure.remove('request')
Expand All @@ -69,6 +79,10 @@ def test_super_closure_edits2():
# we can remove the 'environment' one
del super_closure[0]
del reflist[0]
if PYTEST_ASYNCIO_FIXTURE:
# remove event_loop_policy and environment
del super_closure[0]
del reflist[0]
assert list(super_closure) == reflist

# now supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@
# + All contributors to <https://github.com/smarie/python-pytest-cases>
#
# License: 3-clause BSD, <https://github.com/smarie/python-pytest-cases/blob/master/LICENSE>
from packaging.version import Version

from pytest_cases.plugin import SuperClosure
from pytest_cases import param_fixture, fixture_union

try:
import pytest_asyncio
except ImportError:
PYTEST_ASYNCIO_FIXTURE = False
else:
PYTEST_ASYNCIO_FIXTURE = Version(pytest_asyncio.__version__) >= Version('0.23.0')


# basic parametrized fixtures
a = param_fixture('a', ['x', 'y'])
b = param_fixture('b', [1, 2])
Expand Down Expand Up @@ -48,7 +58,7 @@ def test_super_closure():

# make sure that the closure tree looks good
assert isinstance(super_closure, SuperClosure)
assert str(super_closure) == """SuperClosure with 4 alternative closures:
ref_str = """SuperClosure with 4 alternative closures:
- ['environment', 'c', 'a', 'request', 'd', 'b'] (filters: c=c[0]=a, d=d[0]=b)
- ['environment', 'c', 'a', 'request', 'd'] (filters: c=c[0]=a, d=d[1]=a)
- ['environment', 'c', 'b', 'request', 'a', 'd'] (filters: c=c[1]=b, d=d[0]=b)
Expand All @@ -64,3 +74,9 @@ def test_super_closure():
- ()
- ()
"""

if PYTEST_ASYNCIO_FIXTURE:
ref_str = ref_str.replace("(environment,", "(event_loop_policy,environment,")
ref_str = ref_str.replace("['environment',", "['event_loop_policy', 'environment',")

assert str(super_closure) == ref_str

0 comments on commit 59ab8d2

Please sign in to comment.