Skip to content

Commit

Permalink
Improved compatibility with other pytest plugins, in particular `py…
Browse files Browse the repository at this point in the history
…test-repeat`, by support removal from fixture closure tree. Fixed #269
  • Loading branch information
Sylvain MARIE committed May 10, 2022
1 parent 40b08e2 commit a45a056
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Expand Up @@ -2,6 +2,7 @@

### 3.6.12 - type hint fix

- Improved compatibility with other `pytest` plugins, in particular `pytest-repeat`, by support removal from fixture closure tree. Fixed [#269](https://github.com/smarie/python-pytest-cases/issues/269).
- Fixed type hint errors detected by `pyright`. Fixed [#270](https://github.com/smarie/python-pytest-cases/issues/270)

### 3.6.11 - bugfix for pytest-xdist and `get_all_cases` API improvement
Expand Down
26 changes: 25 additions & 1 deletion src/pytest_cases/plugin.py
Expand Up @@ -640,13 +640,21 @@ def __setitem__(self, i, o):
return
else:
if isinstance(i, slice):
if set(o) == set(ref):
new_set = set(o)
ref_set = set(ref)
if new_set == ref_set:
# a change is required in the order of fixtures. Ignore but continue
warn("WARNING: An attempt was made to reorder a super fixture closure with unions. This is not yet "
"supported since the partitions use subsets of the fixtures ; please report it so that we can "
"find a suitable solution for your need.")
return

added = new_set.difference(ref_set)
removed = ref_set.difference(new_set)
self.append_all(added)
self.remove_all(removed)
return

# At least one change of fixture name is required: not supported, and reject as it is
raise NotImplementedError("It is not possible to replace an element in a super fixture closure,"
"as the partitions inside it do not have the same size")
Expand Down Expand Up @@ -695,6 +703,14 @@ def insert(self, index, fixture_name):
# Finally update self.fixture_defs so that the "list" view reflects the changes in self.tree
self._update_fixture_defs()

def append_all(self, fixture_names):
"""Append various fixture names to the closure"""
# appending is natively supported in our tree growing method
self.tree.build_closure(tuple(fixture_names))

# Finally update self.fixture_defs so that the "list" view reflects the changes in self.tree
self._update_fixture_defs()

def remove(self, value):
"""
Try to transparently support removal. Note: since the underlying structure is a tree,
Expand All @@ -709,6 +725,14 @@ def remove(self, value):
# update fixture defs
self._update_fixture_defs()

def remove_all(self, values):
"""Multiple `remove` operations at once."""
# remove in the tree
self.tree.remove_fixtures(tuple(values))

# update fixture defs
self._update_fixture_defs()


def getfixtureclosure(fm, fixturenames, parentnode, ignore_args=()):
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/pytest_extension/issues/test_issue_269.py
Expand Up @@ -40,6 +40,6 @@ def test_repeat(arg):
def test_synthesis(module_results_dct):
"""Make sure that two tests were created."""
assert list(module_results_dct) == [
"test_repeat-1-2",
"test_repeat-2-2"
"test_repeat[my_fix-1-2]",
"test_repeat[my_fix-2-2]"
]

0 comments on commit a45a056

Please sign in to comment.