-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When selecting tests based on node id's, optimize setup_module() and teardown_module if the tests belong to the same module #3358
Comments
GitMate.io thinks possibly related issues are #499 (Selecting a test by its node id not documented), #50 (Select tests according to their mark), #1360 (Simplify running parametrized tests by node ID), #1978 (pytest not finding any tests under module), and #723 (module scope fixture invoked multiple times if using test node specifiers for same module). |
I think the best way to solve this is to use fixtures rather than xunit-style setup and teardown methods. Would that be a feasible refactor? |
the actual issue that when 2 node ids are given, then py.test collects against the module multiple time (downright duplication of tests) this is a problematic design issue |
@RonnyPfannschmidt : Thanks for your reply. At this moment, do you think I should implement one of the collection hooks like pytest_collection_modifyitems() or pytest_collection(). |
I see. Instead of independently calling It seems from the "remarks" section at the bottom of the "classic xunit-style setup" section of the docs that this is a known issue. I'm guessing that it's a design issue to which attention was consciously diverted, given that the fixture style of setup/teardown management is preferred. The possible outright duplication of nodes is more problematic, although it could be argued that the current behavior is the desired one, and one should use at his own risk. If so, it should be more clearly documented though. |
@brianmaissy the current behaviour is most likely the result of minimal change that was only test driven and not design driven |
@RonnyPfannschmidt : So what approach would you recommend me for this case? Implementing which collection hook : pytest_collection_modifyitems ? or if not this hook, which would be the other one? |
Indeed it is not related to xunit-style at all, I get the same behavior if I use an autouse fixture: import pytest
@pytest.fixture(scope='module', autouse=True)
def _setup_module():
print('SETUP MODULE')
def test_1():
pass
def test_2():
pass
xref: #2679, #723 is a duplicate It might be simplistic, but can't we just remember during argument parsing which modules were loaded and reuse the module object for collection when a module appears twice? |
that should be relatively simple - we should keep a plugin object to store the data somewhere |
Yep, you are right, thanks for the heads up. |
… Note this is still broken due to pytest-dev#3358.
…t to the initial argument(s). Address pytest-dev#3358 by caching nodes in a session dict.
This was fixed by fedc785. |
Following is the structure of my test script:
If I want to run as:
then,
setup_module()
andteardown_module
is run twice because the collection has assumed the node id's belong to different module. However, in this case I don't want to use -k option because I have a bunch of test cases from different classes but from the same script to be selected and the function names could be same in different classes.Is there a way we can have collection hook to figure out if the node id's specified on the cmd line belong to the same module or same class and if yes, execute
setup_module
,setup_class
,teardown_class
andteardown_module
only once?The text was updated successfully, but these errors were encountered: