You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
yield tests are deprecated in pytest and we are getting many warnings in test logs, See e.g.
sklearn/datasets/tests/test_svmlight_format.py::test_load_with_offsets
yield tests are deprecated, and scheduled to be removed in pytest 4.0
sklearn/decomposition/tests/test_online_lda.py::test_verbosity
yield tests are deprecated, and scheduled to be removed in pytest 4.0
sklearn/decomposition/tests/test_pca.py::test_pca_dtype_preservation
yield tests are deprecated, and scheduled to be removed in pytest 4.0
sklearn/ensemble/tests/test_forest.py::test_classification_toy
yield tests are deprecated, and scheduled to be removed in pytest 4.0
sklearn/ensemble/tests/test_forest.py::test_iris
yield tests are deprecated, and scheduled to be removed in pytest 4.0
Allso all the assert_equal, assert_true etc (e.g. here) can be removed now that pytest is used for tests. There must be an issue on it somewhere but I'm not able to find it.
Or was it decided that e.g. running nose2pytest on the whole repo was not desirable as it would create conflicts for existing PRs?
In #8321, @HolgerPeters made some/all of these changes. (See
#7319 (comment))
We
weren't ready for them then. We should take on the yield test changes now,
file by file, if possible.
There is, however, an easier way to transition yield tests than that
proposed in #8321 (making use of parametrize): simply execute the function
instead of yielding it.
Since we usually add rather than modify tests in PRs, I'm not averse to
wholesale conversion of assert_true to assert, etc. (By the same logic I
should not be averse to wholesale autopep8 of test files.) It will still
make conflicts for several PRs, though. But conflict resolution is hardly
the development bottleneck here, nor will these be hard conflicts to solve.
IMO the most painful place to do it is in estimator_checks because estimator_checks can be used outside of a pytest run e.g. by downstream packages (look at e.g. imblearn for an example) or just by calling check_estimator. Something I realised while looking at #10663 is the fact that estimator_checks can be used as a module outside of a pytest run makes it a bit akward to use pytest tricks (in my case I wished to use the tmpdir fixture but that was just not easily doable).
One promising approach I can remember was #7319 (comment) although I have not looked at it in too many details.
About removal of assert_true, assert_equal, I find it hard to really care about this I have to say, but if someone want to run nose2pytest (or something else) to automate the bulk of it, why not ... in a separate PR too though.
I don't think we should do the yield changes and the assert_* changes at
the same time. The advantages of changing assert_equal are:
* sets a good example for new contributions, saving us review time in
asking them to change;
* means that pytest controls the display of failure messages (e.g. handling
of long strings in unittest's assert_equal requires modifying the class's
maxDiff, while pytest uses its command-line flags etc.).
I'm +1 for first removing yield and I'm +1 for @jnothman's way of doing this : simply execute the function instead of yielding it (like numpy/numpy#10294). yield has been deprecated and I don't think removing it in this way will introduce too many conflicts in existing PRs.
Just to be explicit, the downside with replacing yield check, args by check(args) is that you have one single test function instead of multiple test functions. This means that the first assert failing with stop the test whereas it is sometimes nice to have all the failures in one go.
Having said that outside of estimator_checks it/test_common is probably fine to do the simplest thing i.e. yield check, args -> check(args).
Reacted by Hanmin Qin, Mohamed Ali Jamaoui, Tom Dupré la Tour and azrdev
Yes, I think we lose a lot in the informativeness of the test structure in
some cases by getting rid of yield in that way. And the tests are then no
longer a good example of how to write tests in pytest. I think we need to
look at examples to see to what extent parametrize is worth it.
The verbosity of the current approach with yield is nice but IMO there is a larger problem in the fact that a significant portion of tests is in common tests (and it would make sense to increase this portion even more) but there is no way currently to run a particular common test for all estimators.
For instance, in the last sprint cc @glemaitre , #11000 should have been straightforward (at least on the purely coding side, ignoring 64/32bit complications for now) : add a common test that checks 32/64 bit preservation then make it pass without breaking the exiting ones. In reality, it was somewhat painful because,
one had to run a large part of common tests just to get that test. This makes iterations slow, so one can forget about TDD and essentially this wastes contributor time
or write a separate not committed test to run this check for all applicable estimators but then we are not sure that we are actually running it on the same estimators as in _yield_non_meta_checks because there is some more selection logic there. Also, that's confusing for new contributors.
There are a few other issues where this could be relevant I think.
def_yield_non_meta_checks(name, estimator)
yieldcheck_aifnameinSOME_LIST:
yieldcheck_b# etc.defcheck_a(name, estimator):
# check something.defcheck_b(name, estimator):
# check something else only applies# to a few estimatorsdefcheck_estimatator(Estimator):
# similar code to test_non_meta_estimators# but running check(name, estimator) instead of# yielding
Desired behavior
Putting aside the fact that yield will be deprecated by pytest, the above allows to
have a separate entry for test_a in the test suite for each estimator it applies to.
be able to run all tests for an estimator with check_estimator both just in a python interpreter without requiring pytest and in the test suite of some other sciki-learn-contrib project.
what it doesn't allow to do is to run e.g. check_a for all estimators. This is problematic as outlined above.
Possible approach
The most straightforward thing, I guess, would be to parametrize check_a, check_a. For instance,
defselector_check_a(name):
returnTruedefselector_check_b(name):
returnnameinSOME_LIST@pytest.mark.parametrize('name, Estimator', [(name, Estimator) forname, Estimatorinall_estimators()ifselector_check_a(name)])deftest_non_meta_estimator_check_a(name, estimator):
check_a(name, estimator)
# same for check_bdefcheck_estimators(Estimator):
name=Estimator.__name__ifselector_check_a(name):
check_a(name, Estimator)
...
# essentially same thing as in _yield_non_meta_checks but with# factorized selectors
This is very rough prototyping, it would be good to associate a check and it's selector in the same object somehow, and improve other things, but in any case, I think, this should get us the desired behavior from the current setup,
also one can use pytest selector -k to run all non_meta_estimator checks,
pytest sklearn -k non_meta_estimator
and the ability to run a single common test for all relevant estimators,
pytest sklearn -k test_non_meta_estimator_check_a
I have not followed in detail the work on estimator tags and it's implication on check_estimator#6715, but I guess the above selectors could be used to filter on some tags as well..
sklearn/utils/estimator_checks.py is left unchanged, and the parametrization relies on _yield_all_checks to produce a list of valid checks for a given estimator.
yield-tests are still used, so this issue is not really complete?
I'm asking from the view of a downstream project, I'm testing my classifier with check_estimator, and would like to see which of its individual tests failed. Doing so under nose by using for check in _yield_all_checks: yield check, name, my_estimator, but py.test won't support this.
This will be included in the not yet released scikit-learn 0.20+, unless you are calling check_estimator with scikit-learn from master?
For downstream projects check_estimator should still work with both pytest and nose. The fact that check_estimator uses yields internally is fine with both.
The issue was about having patterns of the form ,
deftest_something():
forcheckinchecks:
yieldcheck
in scikit-learn tests, these have been now replaced with pytest parametric tests but it should have no incidence on downstream projects.
For downstream projects check_estimator should still work with both pytest and nose.
Yes it works, but if a single test from check_estimator fails all the others are not run. I think that's bad for debugging the own estimator (you never know how many/which things will come up after you fixed the current test, or maybe how the failures are related), so I disassembled the function (for check in _yield_all_checks: yield check, name, my_estimator), making nose run all these separately.
So actually yes, I mangled this issue with my own (stemming from using that private function), sorry for that. I should open another issue to re-fit check_estimator for being run "in pieces" as I suggested, shouldn't I?
Activity
rth commentedon Feb 28, 2018
Allso all the
assert_equal,assert_trueetc (e.g. here) can be removed now that pytest is used for tests. There must be an issue on it somewhere but I'm not able to find it.Or was it decided that e.g. running
nose2pyteston the whole repo was not desirable as it would create conflicts for existing PRs?jnothman commentedon Feb 28, 2018
jnothman commentedon Feb 28, 2018
jnothman commentedon Feb 28, 2018
Merge conflicts with parametrize-based fixes to yield tests will be hard to resolve...
rth commentedon Feb 28, 2018
Thanks for the additional details @jnothman @lesteve !
Maybe module by module would be easier to follow (apart for the estimator_checks)?
Here is a list of all test files (~29) that use yield (this can have some false positives, feel free to edit this comment).
lesteve commentedon Feb 28, 2018
IMO the most painful place to do it is in estimator_checks because estimator_checks can be used outside of a pytest run e.g. by downstream packages (look at e.g. imblearn for an example) or just by calling
check_estimator. Something I realised while looking at #10663 is the fact that estimator_checks can be used as a module outside of a pytest run makes it a bit akward to use pytest tricks (in my case I wished to use the tmpdir fixture but that was just not easily doable).One promising approach I can remember was #7319 (comment) although I have not looked at it in too many details.
tmpdirortmpdir_factoryfixture. That could go in a separate PR too please.jnothman commentedon Feb 28, 2018
qinhanmin2014 commentedon Feb 28, 2018
I'm +1 for first removing
yieldand I'm +1 for @jnothman's way of doing this : simply execute the function instead of yielding it (like numpy/numpy#10294).yieldhas been deprecated and I don't think removing it in this way will introduce too many conflicts in existing PRs.lesteve commentedon Feb 28, 2018
Just to be explicit, the downside with replacing
yield check, argsbycheck(args)is that you have one single test function instead of multiple test functions. This means that the first assert failing with stop the test whereas it is sometimes nice to have all the failures in one go.Having said that outside of estimator_checks it/test_common is probably fine to do the simplest thing i.e.
yield check, args->check(args).jnothman commentedon Feb 28, 2018
rth commentedon Apr 28, 2018
The verbosity of the current approach with
yieldis nice but IMO there is a larger problem in the fact that a significant portion of tests is in common tests (and it would make sense to increase this portion even more) but there is no way currently to run a particular common test for all estimators.For instance, in the last sprint cc @glemaitre , #11000 should have been straightforward (at least on the purely coding side, ignoring 64/32bit complications for now) : add a common test that checks 32/64 bit preservation then make it pass without breaking the exiting ones. In reality, it was somewhat painful because,
_yield_non_meta_checksbecause there is some more selection logic there. Also, that's confusing for new contributors.There are a few other issues where this could be relevant I think.
Current situation
(simplified)
sklearn/tests/test_common.py
sklearn/utils/estimator_checks.py
Desired behavior
Putting aside the fact that yield will be deprecated by pytest, the above allows to
test_ain the test suite for each estimator it applies to.check_estimatorboth just in a python interpreter without requiring pytest and in the test suite of some other sciki-learn-contrib project.what it doesn't allow to do is to run e.g.
check_afor all estimators. This is problematic as outlined above.Possible approach
The most straightforward thing, I guess, would be to parametrize
check_a,check_a. For instance,This is very rough prototyping, it would be good to associate a check and it's selector in the same object somehow, and improve other things, but in any case, I think, this should get us the desired behavior from the current setup,
-kto run all non_meta_estimator checks,I have not followed in detail the work on estimator tags and it's implication on
check_estimator#6715, but I guess the above selectors could be used to filter on some tags as well..What do you think?
rth commentedon May 3, 2018
My message above fails to account for several constraints, but an improved approach that should work can be found in #11063
jnothman commentedon Jun 9, 2018
azrdev commentedon Jul 17, 2018
As stated in #11063 (comment):
yield-tests are still used, so this issue is not really complete?
I'm asking from the view of a downstream project, I'm testing my classifier with
check_estimator, and would like to see which of its individual tests failed. Doing so under nose by usingfor check in _yield_all_checks: yield check, name, my_estimator, but py.test won't support this.rth commentedon Jul 17, 2018
This will be included in the not yet released scikit-learn 0.20+, unless you are calling
check_estimatorwith scikit-learn from master?For downstream projects
check_estimatorshould still work with both pytest and nose. The fact thatcheck_estimatoruses yields internally is fine with both.The issue was about having patterns of the form ,
in scikit-learn tests, these have been now replaced with pytest parametric tests but it should have no incidence on downstream projects.
azrdev commentedon Jul 17, 2018
Yes it works, but if a single test from
check_estimatorfails all the others are not run. I think that's bad for debugging the own estimator (you never know how many/which things will come up after you fixed the current test, or maybe how the failures are related), so I disassembled the function (for check in _yield_all_checks: yield check, name, my_estimator), making nose run all these separately.So actually yes, I mangled this issue with my own (stemming from using that private function), sorry for that. I should open another issue to re-fit
check_estimatorfor being run "in pieces" as I suggested, shouldn't I?rth commentedon Jul 17, 2018
It a valid concern, if you could open a separate issue about it it would be great! Thank you!