Skip to content

Commit 6ed4c3e

Browse files
author
Sylvain MARIE
committed
Fixed empty id bug with old versions of pytest.
1 parent f8db27f commit 6ed4c3e

File tree

3 files changed

+48
-36
lines changed

3 files changed

+48
-36
lines changed

pytest_cases/fixture_parametrize_plus.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -520,39 +520,48 @@ def get_alternative_id(self):
520520
return mini_idvalset(self.argnames, argval, idx=self.alternative_index)
521521

522522

523-
if PYTEST54_OR_GREATER:
524-
# an empty string will be taken into account but NOT filtered out in CallSpec2.id.
525-
# so instead we create a dedicated unique string and return it.
526-
# Ugly but the only viable alternative seems worse: it would be to return an empty string
527-
# and in `remove_empty_ids` to always remove all empty strings (not necessary the ones set by us).
528-
# That is too much of a change.
523+
# if PYTEST54_OR_GREATER:
524+
# # an empty string will be taken into account but NOT filtered out in CallSpec2.id.
525+
# # so instead we create a dedicated unique string and return it.
526+
# # Ugly but the only viable alternative seems worse: it would be to return an empty string
527+
# # and in `remove_empty_ids` to always remove all empty strings (not necessary the ones set by us).
528+
# # That is too much of a change.
529529

530-
EMPTY_ID = "13h#987__36a721f8Z44526!8*72"
530+
EMPTY_ID = "<pytest_cases_empty_id>"
531531

532+
533+
if has_pytest_param:
532534
def remove_empty_ids(callspec):
533535
# used by plugin.py to remove the EMPTY_ID from the callspecs
534536
callspec._idlist = [c for c in callspec._idlist if not c.startswith(EMPTY_ID)]
535-
536-
elif PYTEST421_OR_GREATER:
537-
# an empty string will be taken into account and filtered out in CallSpec2.id.
538-
EMPTY_ID = ""
539-
540537
else:
541-
# an empty string will only be taken into account if its truth value is True
542-
# it will be filtered out in CallSpec2.id
543-
class EmptyId(str):
544-
def __new__(cls):
545-
return str.__new__(cls, "")
546-
547-
def __nonzero__(self):
548-
# python 2
549-
return True
538+
def remove_empty_ids(callspec):
539+
# used by plugin.py to remove the EMPTY_ID from the callspecs
540+
callspec._idlist = [c for c in callspec._idlist if not c.endswith(EMPTY_ID)]
550541

551-
def __bool__(self):
552-
# python 3
553-
return True
554542

555-
EMPTY_ID = EmptyId()
543+
# elif PYTEST421_OR_GREATER:
544+
# # an empty string will be taken into account and filtered out in CallSpec2.id.
545+
# # but.... if this empty string appears several times in the tests it is appended with a number to become unique :(
546+
# EMPTY_ID = ""
547+
#
548+
# else:
549+
# # an empty string will only be taken into account if its truth value is True
550+
# # but.... if this empty string appears several times in the tests it is appended with a number to become unique :(
551+
# # it will be filtered out in CallSpec2.id
552+
# class EmptyId(str):
553+
# def __new__(cls):
554+
# return str.__new__(cls, "")
555+
#
556+
# def __nonzero__(self):
557+
# # python 2
558+
# return True
559+
#
560+
# def __bool__(self):
561+
# # python 3
562+
# return True
563+
#
564+
# EMPTY_ID = EmptyId()
556565

557566

558567
class ParamIdMakers(UnionIdMakers):

pytest_cases/plugin.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333

3434
from .fixture_core1_unions import NOT_USED, is_fixture_union_params, UnionFixtureAlternative
3535

36-
if PYTEST54_OR_GREATER:
37-
# we will need to clean the empty ids explicitly in the plugin :'(
38-
from .fixture_parametrize_plus import remove_empty_ids
36+
# if PYTEST54_OR_GREATER:
37+
# # we will need to clean the empty ids explicitly in the plugin :'(
38+
from .fixture_parametrize_plus import remove_empty_ids
39+
3940

4041
_DEBUG = False
4142

@@ -870,9 +871,9 @@ def create_call_list_from_pending_parametrizations(self):
870871
for c in calls]) + "\n")
871872

872873
# clean EMPTY_ID set by @parametrize when there is at least a MultiParamsAlternative
873-
if PYTEST54_OR_GREATER:
874-
for callspec in calls:
875-
remove_empty_ids(callspec)
874+
# if PYTEST54_OR_GREATER:
875+
for callspec in calls:
876+
remove_empty_ids(callspec)
876877

877878
# save the list and put back self as the _calls facade
878879
self._call_list = calls

pytest_cases/tests/cases/issues/test_issue_171.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
2-
from pytest_cases import case, parametrize_with_cases
2+
3+
from pytest_cases.common_pytest_marks import has_pytest_param
4+
from pytest_cases import case, parametrize_with_cases, fixture
35

46

57
class CreateCases(object):
@@ -68,7 +70,7 @@ def case_create_9(self):
6870
return inputs, request_data
6971

7072

71-
@pytest.fixture
73+
@fixture
7274
def validationOff():
7375
# app.config["validation"] = False
7476
yield
@@ -84,12 +86,12 @@ def test_synthesis(module_results_dct):
8486
assert list(module_results_dct) == [
8587
'test_create[current_object]',
8688
'test_create[lala]',
87-
'test_create[without_validation]',
89+
'test_create[%swithout_validation]' % ("" if has_pytest_param else "1"),
8890
'test_create[history_object]',
8991
'test_create[xml_with_namespaces]',
9092
'test_create[duplicate_name]',
91-
'test_create[duplicate_name_without_valid]',
93+
'test_create[%sduplicate_name_without_valid]' % ("" if has_pytest_param else "3"),
9294
'test_create[not_enough_namespaceses]',
9395
'test_create[invalid_xml]',
94-
'test_create[invalid_xml_without_valid]'
96+
'test_create[%sinvalid_xml_without_valid]' % ("" if has_pytest_param else "5"),
9597
]

0 commit comments

Comments
 (0)