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

Switching to pytest for unit testing framework; Use doctest more extensively #947

Closed
jthielen opened this issue Dec 21, 2019 · 17 comments · Fixed by #1235
Closed

Switching to pytest for unit testing framework; Use doctest more extensively #947

jthielen opened this issue Dec 21, 2019 · 17 comments · Fixed by #1235
Milestone

Comments

@jthielen
Copy link
Contributor

Would there be support for moving to pytest as the unit testing framework for Pint over unittest? I think this would be a worthwhile move for a couple reasons:

@hgrecco What are your thoughts on this?

@hgrecco
Copy link
Owner

hgrecco commented Dec 21, 2019

I am +1 on this.

@hgrecco
Copy link
Owner

hgrecco commented Dec 21, 2019

I would also like to migrate to Numpy docs and use doctest more extensively to make sure that the documentation is always up to date.

@jthielen
Copy link
Contributor Author

In #954, I'm working on switching Travis CI to use pytest instead of unittest. However, I think this issue should remain open for the reminder on migrating to NumPy docs and using doctest more extensively?

bors bot added a commit that referenced this issue Dec 26, 2019
954: Switch Travis CI test configuration to pytest and add matplotlib tests r=hgrecco a=jthielen

As discussed in #947, this PR switches Travis CI to use pytest for Pint's test suite instead of unittest. As a motivating example, basic tests for `pint.matplotlib` are added (which closes #897).

- [x] Closes #897, towards #947 (but does not fully resolve it)
- [x] Executed ``black -t py36 . && isort -rc . && flake8`` with no errors
- [x] The change is fully covered by automated unit tests
- ~~Documented in docs/ as appropriate~~
- [x] Added an entry to the CHANGES file


Co-authored-by: Jon Thielen <github@jont.cc>
@hgrecco
Copy link
Owner

hgrecco commented Dec 26, 2019

However, I think this issue should remain open for the reminder on migrating to NumPy docs and using doctest more extensively?

I agree

@hgrecco
Copy link
Owner

hgrecco commented Dec 26, 2019

I am trying pyment to migrate the docs

@jthielen jthielen changed the title Switching to pytest for unit testing framework Switching to pytest for unit testing framework; Use doctest more extensively Dec 27, 2019
@hgrecco hgrecco added this to the 0.10 milestone Dec 27, 2019
bors bot added a commit that referenced this issue Dec 27, 2019
958: Migrates docstring to Numpy docs r=hgrecco a=hgrecco

As defined here https://numpydoc.readthedocs.io/en/latest/format.html

- [ ] ~~Closes # (insert issue number)~~
- [x] Executed ``black -t py36 . && isort -rc . && flake8`` with no errors
- [ ] ~~The change is fully covered by automated unit tests~~
- [ ] ~~Documented in docs/ as appropriate~~
- [x] Added an entry to the CHANGES file

See #947

Co-authored-by: Hernan <hernan.grecco@gmail.com>
@hgrecco
Copy link
Owner

hgrecco commented Jan 6, 2020

While this is partly complete, I am phasing this to 0.11 because I think we should make a more in depth change before closing this issue.

@hgrecco hgrecco modified the milestones: 0.10, 0.11 Jan 6, 2020
@hgrecco hgrecco modified the milestones: 0.11, 0.12 Feb 10, 2020
@clarkgwillison
Copy link
Contributor

@hgrecco What's the recommended way of running the doctests? (Or even the whole test suite in general?)

If I run naked pytest on the current master (cda2a40) I get 1808 tests collected and no failures, but if I run

$ python -c 'from pint.testsuite import test_docs; test_docs()'

I get 18 tests and 11 failures, and the output looks like legitimate (albeit mostly minor) failures, which makes me wonder if doctests are being skipped in CI.

None of the following invocations replicates that behavior, so I'm at a bit of a loss:

$ pytest pint/testsuite/__init__.py::test_docs     # ERROR: not found
$ pytest pint/testsuite/__init__.py                # doesn't seem to collect doctests
$ pytest -k 'test_docs'                            # collects 1808, deselects all 1808

(Happy to submit a PR updating developer docs with the recommended local environment setup and test suite invocations)

@keewis
Copy link
Contributor

keewis commented Jun 16, 2020

I would use

python -m pytest --doctest-modules --ignore=pint/testsuite

which I believe will run the doctests it can find (remove the --ignore if you want to run the testsuite, too). To get it to work, the inconsistent whitespace in pint.registry.ContextRegistry.with_context has to be fixed. After that, I only get 4 (functions with) doctests...

@clarkgwillison
Copy link
Contributor

clarkgwillison commented Jun 17, 2020

Hmm, that works to run the tests in the module docstrings, but not for any of the .. doctest:: callouts in the Sphinx documentation. Poking around, it looks like the latter is best run with:

$ cd docs
$ make doctest

Which, as of f8ec2ca is giving me:

[.... lots of useful output]

Doctest summary
===============
  314 tests
   92 failures in tests
    0 failures in setup code
    0 failures in cleanup code
build finished with problems, 3 warnings.
make: *** [doctest] Error 1

Which are more believable numbers given there's 209 blocks marked with .. doctest:: in the docs.

If that is indeed the correct way of running the Sphinx doctests, it looks like we need to:

  • clean up those 92 failures
  • update the developer reference to help people run them locally, and
  • add $ make doctest to the Travis configuration

@hgrecco
Copy link
Owner

hgrecco commented Jun 17, 2020

It would also be good to see double check that all the docstrings are writen using the Numpy style, and the API docs are nicely generated.

@clarkgwillison
Copy link
Contributor

It looks like the package flake8-docstrings might help there, though we get a very large number of errors. I ran it just now and got the following:

$ pip install flake8-docstrings
$ flake8 --docstring-convention numpy | egrep -o "\W([D]\d{3}.*$)" | sort | uniq -c | sort -r
 716  D102 Missing docstring in public method
 123  D105 Missing docstring in magic method
  92  D400 First line should end with a period
  72  D101 Missing docstring in public class
  65  D103 Missing docstring in public function
  62  D205 1 blank line required between summary line and description
  52  D414 Section has no content
  45  D202 No blank lines allowed after function docstring
  30  D200 One-line docstring should fit on one line with quotes
  25  D100 Missing docstring in public module
  24  D401 First line should be in imperative mood
  17  D208 Docstring is over-indented
   9  D401 First line should be in imperative mood; try rephrasing
   4  D403 First word of the first line should be properly capitalized
   3  D210 No whitespaces allowed surrounding docstring text
   2  D404 First word of the docstring should not be `This`
   2  D300 Use """triple double quotes"""
   1  D412 No blank lines allowed between a section header and its content
   1  D209 Multi-line docstring closing quotes should be on a separate line
   1  D104 Missing docstring in public package

Perhaps if we suppressed D102 and D105 the rest would be manageable?
Related: #972

bors bot added a commit that referenced this issue Jun 17, 2020
1116: Harmonize most doctests with Pint's current behavior r=hgrecco a=clarkgwillison

- [ ] Closes # (no single issue)
- [x] Executed ``black -t py36 . && isort -rc . && flake8`` with no errors
- [x] The change is fully covered by automated unit tests
- [x] Documented in docs/ as appropriate
- [x] Added an entry to the CHANGES file

This PR partially addresses #947, #972, and #990 

After merging, the number of failing doctests in the Sphinx documentation should go from 92 (as mentioned in #947) down to 3:
```
Doctest summary
===============
  335 tests
    3 failures in tests
    0 failures in setup code
    0 failures in cleanup code
build finished with problems.
make: *** [doctest] Error 1
```
Which will put us well in reach of enabling doctests in Travis to prevent documentation regressions in the future.

Most tests were fixed in this PR by deferring to the current behavior of Pint, however `Quantity.__repr__()` was modified to round floating point magnitudes to 9 digits to avoid several test failures that were being caused by floating point ambiguities.

Issue #1115 was opened to track the 3 tests that I could not easily resolve. Once that issue is resolved, we can enable doctests in Travis without breaking CI.

Co-authored-by: Clark Willison <clarkgwillison@gmail.com>
@hgrecco
Copy link
Owner

hgrecco commented Jun 17, 2020

@clarkgwillison I was not aware of this tool. Thanks for the pointing this out.

I was surprised by the result because I knew that most of the code is documented. Then I realized that the test folder was counted. Ignoring the testsuite folder provides a more accurate result:

flake8 --docstring-convention numpy --exclude testsuite | egrep -o "\W([D]\d{3}.*$)" | sort | uniq -c | sort -r
 123  D105 Missing docstring in magic method
  80  D400 First line should end with a period
  78  D102 Missing docstring in public method
  59  D205 1 blank line required between summary line and description
  45  D202 No blank lines allowed after function docstring
  28  D414 Section has no content
  28  D200 One-line docstring should fit on one line with quotes
  27  D103 Missing docstring in public function
  21  D401 First line should be in imperative mood
  17  D208 Docstring is over-indented
   9  D401 First line should be in imperative mood; try rephrasing
   8  D101 Missing docstring in public class
   5  D100 Missing docstring in public module
   4  D403 First word of the first line should be properly capitalized
   3  D210 No whitespaces allowed surrounding docstring text
   2  D404 First word of the docstring should not be `This`
   2  D300 Use """triple double quotes"""
   1  D412 No blank lines allowed between a section header and its content

Having said this, I would agree to ignore D105.

@hgrecco
Copy link
Owner

hgrecco commented Jun 18, 2020

or better:
flake8 --docstring-convention numpy --exclude testsuite,bench,docs

@clarkgwillison
Copy link
Contributor

Nice, that does make the numbers look a lot better.

Given the amount of work to get to zero here, it might make sense to go for a longer term goal (like getting to coverage 100%)

Is there a way to implement "check the # errors didn't increase" rather than "check that # errors is zero" in Travis?

@hgrecco
Copy link
Owner

hgrecco commented Jun 22, 2020

I don't think that is possible. My suggestion is that you add this test and label it as expected failure. Having that reporting this would be enough for now.

@cpascual
Copy link
Contributor

Is there a way to implement "check the # errors didn't increase" rather than "check that # errors is zero" in Travis?

Maybe it can be done with a bit of customization, by telling flake8 to check only the diffs. We do something like that (for flake8, not flake8-docstings) here

@hgrecco
Copy link
Owner

hgrecco commented Jan 10, 2021

I have just pushed a new branch _pytest_migration in which unittest is replaces by pytest. While there is still room for improvement, all tests are passing, and it is 25% faster.

If there are now complains, I will rebase it on top of master and merge it on Jan 17.

Just to summarize what can be improved:

  • This way of combining marks does not work:
    def multi_mark(*funcs):
  • pytest-subtests has been used a lot but parametrize should be better in multiple cases.
  • Properly scoped fixtures should be used to speed up testing
  • Some classes could be removed and use plain functions.

@hgrecco hgrecco mentioned this issue Jan 16, 2021
5 tasks
bors bot added a commit that referenced this issue Jan 17, 2021
1235: Pytest migration r=hgrecco a=hgrecco

- [x] Closes #947  (insert issue number)
- [x] Executed ``pre-commit run --all-files`` with no errors
- [x] The change is fully covered by automated unit tests
- [x] Documented in docs/ as appropriate
- [x] Added an entry to the CHANGES file


Co-authored-by: Hernan <hernan.grecco@gmail.com>
Co-authored-by: Hernan Grecco <hernan.grecco@gmail.com>
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 a pull request may close this issue.

5 participants