Skip to content

Commit 5076f32

Browse files
author
Sylvain MARIE
committed
Fixed bug when a test module containing @parametrize_with_cases was executed outside of pytest, typically through its __main__. Fixes #198
1 parent 573928c commit 5076f32

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

pytest_cases/common_pytest.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import inspect
88
import sys
9+
import os
910
from importlib import import_module
1011

1112
from makefun import add_signature_parameters, wraps
@@ -30,7 +31,7 @@
3031
from .common_others import get_function_host
3132
from .common_pytest_marks import make_marked_parameter_value, get_param_argnames_as_list, \
3233
get_pytest_parametrize_marks, get_pytest_usefixture_marks, PYTEST3_OR_GREATER, PYTEST6_OR_GREATER, \
33-
PYTEST38_OR_GREATER, PYTEST34_OR_GREATER, PYTEST33_OR_GREATER
34+
PYTEST38_OR_GREATER, PYTEST34_OR_GREATER, PYTEST33_OR_GREATER, PYTEST32_OR_GREATER
3435
from .common_pytest_lazy_values import is_lazy_value, is_lazy
3536

3637

@@ -65,6 +66,18 @@ def _decorate(f):
6566
return _decorate
6667

6768

69+
def pytest_is_running():
70+
"""Return True if the current process is a pytest run
71+
72+
See https://stackoverflow.com/questions/25188119/test-if-code-is-executed-from-within-a-py-test-session
73+
"""
74+
if PYTEST32_OR_GREATER:
75+
return "PYTEST_CURRENT_TEST" in os.environ
76+
else:
77+
import re
78+
return any(re.findall(r'pytest|py.test', sys.argv[0]))
79+
80+
6881
def remove_duplicates(lst):
6982
dset = set()
7083
# relies on the fact that dset.add() always returns None.
@@ -619,7 +632,9 @@ def __init__(self, func):
619632

620633
self.config = PYTEST_CONFIG
621634
if self.config is None:
622-
raise ValueError("Internal error - config has not been correctly loaded. Please report")
635+
# only raise if we are in pytest, otherwise silently skip
636+
if pytest_is_running():
637+
raise ValueError("Internal error - config has not been correctly loaded. Please report")
623638

624639
self.function = func
625640
self.definition = MiniFuncDef(func.__name__)

pytest_cases/common_pytest_marks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
PYTEST_VERSION = LooseVersion(pytest.__version__)
3131
PYTEST3_OR_GREATER = PYTEST_VERSION >= LooseVersion('3.0.0')
32+
PYTEST32_OR_GREATER = PYTEST_VERSION >= LooseVersion('3.2.0')
3233
PYTEST33_OR_GREATER = PYTEST_VERSION >= LooseVersion('3.3.0')
3334
PYTEST34_OR_GREATER = PYTEST_VERSION >= LooseVersion('3.4.0')
3435
PYTEST35_OR_GREATER = PYTEST_VERSION >= LooseVersion('3.5.0')

0 commit comments

Comments
 (0)