Skip to content
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

Switch to pytest and tox #1763

Merged
merged 5 commits into from Oct 19, 2020
Merged

Conversation

saroad2
Copy link
Contributor

@saroad2 saroad2 commented Oct 14, 2020

It became a good practice in python to use Tox. In that way we can make sure that the contributors of black test in the same environment as the CI process.

Moreover, I propose moving to use Pytest as the testing infrastructure because it could make our tests much clearer. Consider the following tests from test_black.py:

    ...
    @patch("black.dump_to_file", dump_to_stderr)
    def test_function(self) -> None:
        source, expected = read_data("function")
        actual = fs(source)
        self.assertFormatEqual(expected, actual)
        black.assert_equivalent(source, actual)
        black.assert_stable(source, actual, DEFAULT_MODE)

    @patch("black.dump_to_file", dump_to_stderr)
    def test_function2(self) -> None:
        source, expected = read_data("function2")
        actual = fs(source)
        self.assertFormatEqual(expected, actual)
        black.assert_equivalent(source, actual)
        black.assert_stable(source, actual, DEFAULT_MODE)
    ...

Those are practically the same test. Why do we need to write it twice? with pytest we could simply write:

    ...
    @patch("black.dump_to_file", dump_to_stderr)
    @pytest.mark.parametrize("file_name", ["function", "function2"])
    def test_function(self, file_name) -> None:
        source, expected = read_data(file_name)
        actual = fs(source)
        self.assertFormatEqual(expected, actual)
        black.assert_equivalent(source, actual)
        black.assert_stable(source, actual, DEFAULT_MODE)
    ...

I think both changes will be beneficial for Black.

What do you think?

Copy link
Collaborator

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

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

Thanks for this. I'm all for making our tests runnable from tox and pytest, but not enforcing the use of pytest in our CI. What I'm not a big fan of is pytest's magic and overloading of standard python functionality (e.g. assert) and that it's now another dependency to black to test. But I'll live with it if other black core devs like pytest, I've had to learn it and deal with it for other projects.

Aside from this, love the refactor and split of blackd tests. Love the clearer deps now too.

Comment on lines 23 to +24
python -m pip install --upgrade pip
python -m pip install --upgrade coverage
python -m pip install --upgrade hypothesmith
python -m pip install -e ".[d]"
python -m pip install --upgrade tox
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not install together?
python -m pip install --upgrade pip tox

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe the newest pip has something new and exciting, and we want to use the new and exciting pip for the other stuff?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Just seems a waste of an extra fork/exec for now ... If we hit a bug then I'd change back to the two executes ... Not a big deal, thus why I didn't request changes ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm with @hugovk on this.

Copy link
Contributor

Choose a reason for hiding this comment

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

If it were a single call to pip, then there's no point upgrading pip (unless it is cached for next time, and it's not).

A concrete reason:

For Python 3.8 on Windows, pip 19.3+ is needed to install wheels (at least for Pillow). We've added the double command to the installation instructions, and that came up again this week.

This isn't specifically needed for Black test right now, but I suggest skipping the pip upgrade altogether and using the CI image's version, or having two calls.

(Right now, pip in the image is the latest version, so the call is really quick; and it's pretty quick for an upgrade as well.)

Comment on lines 24 to +25
python -m pip install --upgrade pip
python -m pip install --upgrade coverage
python -m pip install -e ".[d]"
python -m pip install --upgrade tox
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same - Install both?

@saroad2
Copy link
Contributor Author

saroad2 commented Oct 14, 2020

I'm not in love with the idea of Pytest, but I have a great experience with it in all of my repositories.

If I cannot use pytest.mark.parametrize, may I suggest using this instead. Never tried it so I don't know how it is, but I think it worths a try. Maybe I can try to move tests to use it in another PR after you merge this one.

@cooperlees
Copy link
Collaborator

I'm not in love with the idea of Pytest, but I have a great experience with it in all of my repositories.

If I'm cannot use pytest.mark.parametrize, may I suggest using this instead. Never tried it so I don't know how it is, but I think it worths a try. Maybe I can try to move tests to use it in another PR after you merge this one.

Yes, let's definitely do the test conversions in a separate PR.

  • Also, lets wait and get other Core Devs for it before doing the conversion. Support is one thing, going pytest only is another.

I just want to state I'm not blocking pytest, but sharing I'm not for it. Mainly due to how much long, due to it's magic it's taken me to debug things. But that's primarily cause I don't use it primarily.

@ichard26
Copy link
Collaborator

While on the topic of In that way we can make sure that the contributors of black test in the same environment as the CI process., perhaps instead of doing a regular pip editable install, a pipenv development install should be done. This makes sure that the CI is using the identical modules and packages that developers of Black use and therefore increasing reproducibility . The other benefits I can think of include:

The main downside is that Black incompatibilities with newer versions of modules / packages won't be automatically discovered since we'll be pinning to probably older versions. Using PyUp or other services mostly mitigates this issue though.

Just some ideas to think about.

@cooperlees
Copy link
Collaborator

While on the topic of In that way we can make sure that the contributors of black test in the same environment as the CI process., perhaps instead of doing a regular pip editable install, a pipenv development install should be done. This makes sure that the CI is using the identical modules and packages that developers of Black use and therefore increasing reproducibility . The other benefits I can think of include:

The main downside is that Black incompatibilities with newer versions of modules / packages won't be automatically discovered since we'll be pinning to probably older versions. Using PyUp or other services mostly mitigates this issue though.

Just some ideas to think about.

If you do the PRs I'm sure we'll merge them :) ...

I love PyUp method, but I always like to have both a pinned and unpinned options. This is due to having situations where I've had to fork to unpin to install things in some environments. And I've not wanted to inflict that pain on users of libraries I've maintained.

Copy link
Collaborator

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

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

This is an improvement to allow for Devs to get a CI like env easier. Lets merge it.

@cooperlees
Copy link
Collaborator

No one has spoke up about not liking this. I'm merging.

@cooperlees cooperlees merged commit 4070527 into psf:master Oct 19, 2020
@saroad2 saroad2 deleted the switch_to_pytest_and_tox branch October 19, 2020 18:11
-r{toxinidir}/test_requirements.txt
hypothesmith
lark-parser < 0.10.0
; lark-parser's version is set due to a bug in hypothesis. Once it solved, that would be fixed.
Copy link
Contributor

Choose a reason for hiding this comment

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

@saroad2 It looks like lark-parser's been updated in the test dependencies for hypothesmith - could/should we remove this version pin?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess we should :) I added this pinning so the tests would pass.

noxan pushed a commit to noxan/black that referenced this pull request Jun 6, 2021
* Add venv to .gitignore

* Use tox to run tests

* Make fuzz run in tox

* Split tests files

* Fix import error
@ichard26
Copy link
Collaborator

Hi @saroad2, thanks for moving us to pytest, it's been a better experience personally. Anyway I'm working on removing parameterized as a test suite dependency and I noticed pytest-mock + pytest-cases. I'm not familiar with these plugins and AFAICT the test suite doesn't use these plugins ATM. If that's the case I'd wish to remove them. Why were they added?

Thank you in advance!

@saroad2
Copy link
Contributor Author

saroad2 commented Sep 21, 2021

Hey @ichard26, I think I added them by mistake.

However, if you take a close look at pytest-cases, I think you'll find it as a very helpful plugin to pytest.
Using it might cause you to rewrite some tests, but in a much prettier form, in my humble opnion.

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.

None yet

5 participants