Skip to content

[MRG+1] Pytest parametrize unit tests (part 1) - ensemble module - #11075

Merged
glemaitre merged 5 commits into
scikit-learn:masterfrom
rth:pytest-parametrize-part1
May 24, 2018
Merged

glemaitre merged 5 commits into
scikit-learn:masterfrom
rth:pytest-parametrize-part1

Conversation

@rth

@rth rth commented May 7, 2018

Copy link
Copy Markdown
Member

Reference Issues/PRs

This is Part 1 of #11074

Please refer to the parent PR for general description and comments.

@rth rth mentioned this pull request May 7, 2018
3 tasks done

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose there are alternative ways to express the same thing, e.g. with fixtures. But this looks fine to me

@rth

rth commented May 8, 2018

Copy link
Copy Markdown
Member Author

I suppose there are alternative ways to express the same thing, e.g. with fixtures.

Yes, you are right, let me see if expressing the frequent parametrizations (here FOREST_CLASSIFIERS, etc) with fixtures would be simpler.

@jnothman

jnothman commented May 8, 2018 via email

Copy link
Copy Markdown
Member

@rth

rth commented May 8, 2018 •

Copy link
Copy Markdown
Member Author

I don't think it's essential, but if we're trying to establish an idiom, we
might as well try get it right.

Yes, I agree that it would be helpful if we could agree on on some convention regarding how tests should ideally be parametrized with pytest in scikit-learn. @lesteve @qinhanmin2014 @amueller @glemaitre

For instance, below is an example of pytest parametrized test,

FOREST_CLASSIFIERS = {
    "ExtraTreesClassifier": ExtraTreesClassifier,
    "RandomForestClassifier": RandomForestClassifier,
}

@pytest.mark.parametrize('name', FOREST_CLASSIFIERS)
def test_some_assumption(name):
    check_some_assumption(name)

it is also possible to express the same thing with a fixture,

@pytest.fixture(params=FOREST_CLASSIFIERS)
def forest_classifiers_fixture(request):
    return request.param

def test_some_assumption(forest_classifiers_fixture):
    check_some_assumption(forest_classifiers_fixture)

when the same parametrization is often used (as is the case in this PR) this might allow to write things of more consisely.

However,

  • I feel there should be some naming convention that specifies that an object is a fixture (here the _fixture suffix), but this makes less nice (too long) variables. Maybe a _fxt suffix would be enough?
  • When the fixture output is re-used several times we have to do,
    def test_some_assumption(forest_classifiers_fixture):
        name = forest_classifiers_fixture
        check_assumption1(name)
        check_assumption2(name)
    or put forest_classifiers_fixture everywhere, both of which I find somewhat awkward.

This PR is an example of using @pytest.mark.parametrize while ab237e9 adapts it to use fixtures (while still keeping parametrize for unfrequent parametrization). In the end, while @pytest.mark.parametrize is more verbose and repetitive I also find it more readable (unless we find a better naming scheme for fixtures)..

WDYT?

@rth

rth commented May 8, 2018

Copy link
Copy Markdown
Member Author

lgtm.com seems to be down..

Another (unrelated) question cc @lesteve is that test_script.sh ensures that sklearn.utils.tests.test_estimator_checks can be run without pytest and in particular that sklearn/utils/testing.py doesn't import it (which definitely makes sense). However, testing.py includes some decorators such as skip_if_32bit that are

  • much easier rewritten with @pytest.mark.skipif (see a581f55)
  • need to be rewritted because otherwise they don't work with pytest parametrize.

So the question is should I put these @pytest.mark.skipif versions e.g. in sklearn/conftest.py and remove them from sklearn/utils/testing.py without deprecation? This means breaking backward compatibility for those but the only reason someone would be using these is for testing and when their tests break they should just change them. Or do we need a deprecation cycle here?

@qinhanmin2014

Copy link
Copy Markdown
Member

I think pytest.fixture will introduce lots of extra work and I can't see too much improvement with it. I doubt whether it's worthy to waste so much time on it.
I've checked the whole PR. LGTM if CIs are green and we reach consensus on previous comment about sklearn/utils/testing.py (I'm OK with current solution).

@rth

rth commented May 8, 2018

Copy link
Copy Markdown
Member Author

I think pytest.fixture will introduce lots of extra work and I can't see too much improvement with it.

Thanks for the feedback and the review! Well, it's not that much extra work... Also, to be specific, this discussion is only about using fixtures for parametrizations.

There are other use cases, where fixtures would definitely make sense IMO, such as loading test datasets (or making random datasets) once and distributing it to all tests that need those. I wonder if by reducing I/O this way tests won't run faster, but that would require a separate investigation..

@rth

rth commented May 15, 2018 •

Copy link
Copy Markdown
Member Author

@lesteve would you have an opinion about #11075 (comment) ?

@rth

rth commented May 16, 2018 •

Copy link
Copy Markdown
Member Author

ping @glemaitre in case you have any opinion on the test style in #11075 (comment)

In the end, this PR doesn't break backward compatibility for nose based decorators to skip tests in sklearn/utils/testing.py (related to #11075 (comment)) I guess we discuss deprecating that later.

Otherwise, it has enough approvals to press the green button )

@glemaitre

glemaitre commented May 16, 2018 via email

Copy link
Copy Markdown
Member

@glemaitre glemaitre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a single nitpick.

Regarding #11075 (comment), I am not sure that we should care about deprecation there. When making contribution (as imblearn) we were force to use nose anyway until 0.19. I am not sure that forcing pytest would be actually an issue, IMO.

Comment thread sklearn/ensemble/tests/test_forest.py Outdated
FOREST_ESTIMATORS.update(FOREST_REGRESSORS)
FOREST_ESTIMATORS.update(FOREST_TRANSFORMERS)

FOREST_CLASSIFIERS_REGRESSORS = dict(FOREST_CLASSIFIERS, **FOREST_REGRESSORS)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got a bit surprise here. Apparently this is a cool hack (maybe not the best to have it in the code base since it working only with key string cf.). I would use update or a dict comprehension.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I wasn't aware that was so much debate online about the right way to merge dicts in Python (see https://stackoverflow.com/a/26853961/1791279). I did like this version but since it's controversial, reverted it to a simpler one. As a side note, as args expansion related hacks go you might like this one which I have seen used in practice )

@rth

rth commented May 17, 2018

Copy link
Copy Markdown
Member Author

Thanks for the feedback!

Addressed your comments. Also following your suggestion replaced the nose specific check_skip_travis and skip_if_32bit with the pytest.mark.skipif in sklearn/utils/testing.py that will be defined only if pytest can be imported. You are right I also can't see a reason to keep the previous versions around...

@glemaitre glemaitre changed the title [MRG] Pytest parametrize unit tests (part 1) - ensemble module [MRG+1] Pytest parametrize unit tests (part 1) - ensemble module May 17, 2018
@glemaitre

Copy link
Copy Markdown
Member

So you have my +1. I would like to have @lesteve and @jnothman opinion before to merge regarding the last 2 comments such that we have a consensus :)

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should I be checking? I'm not entirely sure we should be changing utils/testing.py, but I can hope no one's relied on it that is not using pytest...?

@rth

rth commented May 17, 2018 •

Copy link
Copy Markdown
Member Author

@jnothman just the diff related to sklearn/utils/testing.py https://github.com/scikit-learn/scikit-learn/pull/11075/files#diff-f0af7b240082b7b0e03078a38121ae1a

True; to summarize,

  • we need to adopt these skip_if* functions for pytest as the existing version doesn't work with parametrizing otherwise
  • there is seemingly no reason to keep the former versions
  • utils/testing.py can only optionally depend on pytest (since it's not a dependency) hence the try: except ImporError: pass

@rth

rth commented May 17, 2018

Copy link
Copy Markdown
Member Author

I mean those functions are only used for testing. So the worst case scenario even if someone was relying on them, is that

  • they made a release and unit tests in this release won't run after a new scikit-learn version is released.
  • this breaks their CI in dev version, but that's probably a good thing considering.

So in general I think it's a week reason to keep unused code in the code base..

@jnothman

jnothman commented May 17, 2018 via email

Copy link
Copy Markdown
Member

@glemaitre
glemaitre merged commit c50d9e8 into scikit-learn:master May 24, 2018
@glemaitre

Copy link
Copy Markdown
Member

Thanks rth!!!

@lesteve

lesteve commented May 24, 2018

Copy link
Copy Markdown
Member

Nice !

@rth
rth deleted the pytest-parametrize-part1 branch May 24, 2018 09:16
@qinhanmin2014

Copy link
Copy Markdown
Member

Learnt a lot from the nice work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants