-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Adding external tests to a collection #421
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
Comments
Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42): Thanks for the initiative. One design issue is how to "override" or "substitute" fixtures. Supose you want to run tests found in |
Original comment by Trevor Bekolay (BitBucket: tbekolay, GitHub: tbekolay): Right, that's a good point, it's very likely (certain?) that there will be some conflicting fixtures. How is this handled right now within a package? I just tried defining a funcarg in a test file and in |
Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42): Right now, fixtures are looked up according to "source order", meaning |
Hi all, I'm looking for a solution to the same problem. The issue is a few years old, are there any updates on this or recommended ways of doing this with pytest today? |
Hi @mloning, Indeed that issue is quite old and lost steam. There are some issues to consider, for example do the external tests have fixtures? I think the best bet would be to manage to create the tests programatically and add them to the collection in |
Hi @nicoddemus, thanks for the reply! The use case is as follows:
I'd really appreciate if you could add a bit more detail on how to work with |
Hi @mloning, Thanks for providing more details. In that case I do have a suggestion on how to run tests from a In the # base/common.py
class BaseConformanceSuite:
@pytest.fixture
def target(self):
"""Returns the object that will be checked for conformance"""
assert 0, "This fixture needs to be re-declared in subclasses"
def test_fit(self, target):
assert target.fit(...)
def test_predict(self, target):
assert target.predict(...) Note the class name does not start or end with You then subclass from # base/tests/test_transform.py
from base.common import BaseConformanceSuite
from base.algorithms import TransformAlgo
class TestTransform(BaseConformanceSuite):
@pytest.fixture
def target(self):
return TransformAlgo() # companion/tests/test_forecaster.py
from base.common import BaseConformanceSuite
from companion.algorithms import ForecasterAlgo
class TestForecaster(BaseConformanceSuite):
@pytest.fixture
def target(self):
return ForecasterAlgo() Note here that they start with In summary, the trick is to use base inheritance to get your tests from one package to another. This doesn't require implementing any hooks, and fixtures/collection will work naturally. |
Thanks @nicoddemus - this is really helpful! I'll get back to you in case we have more questions or get stuck, but I think I know what we have to do now. |
@nicoddemus one follow-up question, is there an easy way to parametrize the fixtures when define as above in the method? Instead of having a single We're currently using class TestForecaster(BaseConformanceSuite):
@pytest.fixture(params=all_forecasters)
def target(self, estimator):
return estimator.param |
Not sure if that snippet is meant to be pseudo-code, but it's pretty much the right syntax. The argument just needs to be named class TestForecaster(BaseConformanceSuite):
@pytest.fixture(params=all_forecasters)
def target(self, request):
return request.param More info on parametrized fixtures: https://docs.pytest.org/en/6.2.x/fixture.html#fixture-parametrize |
Thanks @kalekundert |
Hi all!
In test_*.py we declare tests that inherit from common_case (since the cases and steps are repeated and differ only in some steps/conditions).
I tried to do it as suggested here, but either I didn’t understand the idea or it didn’t suit me. |
The idea is for you to implement your own Take a look at pytest-cpp for an example of a plugin which creates new tests from external resources. |
Originally reported by: Trevor Bekolay (BitBucket: tbekolay, GitHub: tbekolay)
It would be nice if it were possible to run tests from outside of the current package when running py.test on the current package. A possible use case for this functionality would be to have a common set of tests for many packages implementing a certain protocol or standard. Another use case is for testing pluggable features (this was the the origin of the idea; see this SO question).
Two things are necessary for this to happen. First, you must get a set of tests contained in another package. Second, you must add that set of tests to the current package.
For getting access to external tests, you can do something similar to what occurs when doing
py.test --collect-only
and providing the path to the external package. However, it should additionally be possible to disable all reporting, and to get programmatic access to the tests collected by that run.For adding external tests to the current collection, one possibility is
pytest_collection_modifyitems
. If the tests can be collected programmatically they can just be added to the items list. However, a more explicit hook likepytest_collection_addexternalitems
or something might be preferable.Since I'd like this for one of my projects, I'm motivated to implement this, but it'd be best to discuss a nice API for doing this before starting an implementation.
The text was updated successfully, but these errors were encountered: