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

#14 Add tests. pytest_watch.command and pytest_watch.watcher #79

Open
wants to merge 93 commits into
base: master
Choose a base branch
from

Conversation

apast
Copy link

@apast apast commented Jan 3, 2018

Hi,

This PR refers to #14 .

I added initial tests over command.py (coverage 98%) and to main.py.

Tests are implemented using basic unittest, and I run it on Linux using python 2 & 3.

I am implementing it from top-down, before try to add or change any funcionality.

General structure of code was keep as the same. I only fixed docopt [default: ...] definitions for --spool and --ext and now we have default values defined by documentation and being set by docopt parser. I updated README.md accordingly.

I mocked pytest_watch.watcher.watch and pytest_watch.config.merge_config to isolate and focus on CLI input and validadion.

Can you review and give me some feedback to improve the code before accept this PR?

Thank you!

@apast apast changed the title 14 add tests Add CLI test coverage (98%) Jan 3, 2018
@apast apast changed the title Add CLI test coverage (98%) Add CLI test coverage (pytest_watch.command: 98%) Jan 4, 2018
@@ -29,7 +29,7 @@
--pdb Start the interactive Python debugger on errors.
This also enables --wait to prevent pdb interruption.
--spool <delay> Re-run after a delay (in milliseconds), allowing for
more file system events to queue up (default: 200 ms).
more file system events to queue up [default: 200].
-p --poll Use polling instead of OS events (useful in VMs).
Copy link
Owner

Choose a reason for hiding this comment

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

Ah, nice removal of the redundant ms.

@@ -11,7 +11,7 @@
--ignore <dir> Ignore directory from being watched and during
collection (multi-allowed).
--ext <exts> Comma-separated list of file extensions that can
trigger a new test run when changed (default: .py).
trigger a new test run when changed [default: .py].
Copy link
Owner

@joeyespo joeyespo Jan 4, 2018

Choose a reason for hiding this comment

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

Are brackets a common for default values in CLI tool --help texts? Nevermind. Just noticed docopt uses brackets in their docs. Thanks for updating 👍

sys.path.append(os.path.dirname(__file__))

from pytest_watch.command import main
main()
main(argv=sys.argv[1:])
Copy link
Owner

Choose a reason for hiding this comment

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

I think main() with no args is more aesthetic. The default is to use argv[1:] and adding that here adds noise.

Could you change it back?

If your rationale was "I couldn't tell what main's default were", a better docstring for main could address that instead of more code.

if args['--ext'] == '*':
extensions = ALL_EXTENSIONS
elif args['--ext']:
extensions = [('.' if not e.startswith('.') else '') + e
for e in args['--ext'].split(',')]
else:
extensions = None
Copy link
Owner

Choose a reason for hiding this comment

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

Ahh I see fixing the default value in docopt allows you to remove this. Very nice 👍

try:
spool = int(spool)
except ValueError:
sys.stderr.write('Error: Spool (--spool %s) must be an integer.\n' % spool)
Copy link
Owner

Choose a reason for hiding this comment

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

Two things.

  1. I used to willfully break PEP8's "80 character limit" rule all the time. With side-by-side comparisons and 3-way merges, I've since come to appreciate this style rule. Could you do the same here?

  2. I thought I read a while back that Python favored .format over % these days. A quick Google search yields no results on this. But since I've since switched to using .format everywhere, could you do the same here for consistency? A case could be made later to go back to % everywhere.

This should do it:

        sys.stderr.write(
            'Error: Spool (--spool {}) must be an integer.\n'.format(spool))

Or perhaps:

        print(
            'Error: Spool (--spool {}) must be an integer.\n'.format(spool),
            file=sys.stderr)


if spool < 0:
sys.stderr.write("Error: Spool value(--spool %s) must be positive integer" % spool)
return 2
Copy link
Owner

Choose a reason for hiding this comment

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

Ahh nice catch! 👍

Also, same comment as above about using 80 characters.

from pytest_watch.__main__ import run_cli


class TestRunCLI(unittest.TestCase):
Copy link
Owner

Choose a reason for hiding this comment

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

Is there any chance you could switch to classless pytest? Using classes just adds overhead IMHO.

If not, I'll probably go back and do this at a (much) later date.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I will adopt this classless where it is unecessary.

ignore = [] if ignore is None else ignore
extensions = [] if extensions is None else extensions
pytest_args = [] if pytest_args is None else pytest_args

Copy link
Owner

@joeyespo joeyespo Jan 4, 2018

Choose a reason for hiding this comment

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

This is much better than what I had 👍

Avoiding any accidental mutation is definitely a good thing. Good eye!

@@ -0,0 +1,437 @@
import sys
Copy link
Owner

@joeyespo joeyespo Jan 4, 2018

Choose a reason for hiding this comment

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

(Same comment in this file about using functions over class-based tests.)

Also assert x != y is much more readable than self.assertFalse(condition), etc. Pytest does an amazing job of inspecting values when things go wrong, so I'm all for the boost in readability using raw assert. Same goes for assert x in y over self.assertIn(x, y) and assert x not in y over self.assertNotIn(x, y)

And again, if you'd rather get this in now, that's fine. I'll probably go back and switch it later on if that's the case.

Copy link
Author

Choose a reason for hiding this comment

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

Perfect. I agree with readability os simple assert. I will adopt this for general assertions.

I am in doubt about self.assertListEqual and self.assertDictEqual calls. I think it is better to use unittest helpers in this case. What do you think?

Thanks

Copy link
Owner

@joeyespo joeyespo Jan 5, 2018

Choose a reason for hiding this comment

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

Well given that pytest advocates the use of assert over self.assert, and this is a pytest-based project, I think using vanilla assert is appropriate.

Here's the top bullet from pytest's Features list:

  • Detailed info on failing assert statements (no need to remember self.assert* names)

FWIW readability and the lack of boilerplate was what attracted me to pytest to begin with.

Copy link
Author

Choose a reason for hiding this comment

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

Done!

@@ -87,23 +91,24 @@ def main(argv=None):
if args['--pdb']:
pytest_args.append('--pdb')

# Parse extensions
# Parse extensions [default: .py]
Copy link
Owner

Choose a reason for hiding this comment

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

Nice addition of comments 👍 Even more so with the defaults right there.

.gitignore Outdated
@@ -14,3 +14,7 @@ env
.DS_Store
Desktop.ini
Thumbs.db
.coverage
.coverage.*
venv
Copy link
Owner

Choose a reason for hiding this comment

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

"Environment file" may be a better place for venv.

I wonder if it'd make sense to add a new "Testing files" for things like .coverage.

Copy link
Author

@apast apast Jan 4, 2018

Choose a reason for hiding this comment

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

About the suggested github .gitignore (https://github.com/github/gitignore/blob/master/Python.gitignore), it is up to date and a more complete version for python related projects.

Can we replace current version by the community version?

Copy link
Owner

@joeyespo joeyespo left a comment

Choose a reason for hiding this comment

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

This looks great! Thank you 😄

Only requested some small change here and there. I think we can get this in quickly after that.

@joeyespo
Copy link
Owner

joeyespo commented Jan 4, 2018

Nice! Just tested on Windows and everything is passing 💯 😌

The only thing I had to do was install pytest-cov. I think we can signal that requirement by:

  • Adding a new requirements-test.txt file

  • Adding pytest-cov>=2.5.1 to it

  • Adding a tests_require field to setup.py:

    tests_require=read('requirements-test.txt').splitlines()
    

    (Note that after doing this, running python setup.py test will succeed even if you don't manually install pytest-cov for pytest to use. This creates a local .eggs directory. May be worth adding that as an entry to .gitignore.)

@joeyespo
Copy link
Owner

joeyespo commented Jan 4, 2018

@blueyed Anything you'd like to add?

@apast
Copy link
Author

apast commented Jan 4, 2018

Hello, Joe,

Thanks for your detailed review.

I will check this and change code or reply properly.

[]'s,

Andre

apast pushed a commit to apast/pytest-watch that referenced this pull request Jan 4, 2018
…ted in joeyespo#79 (review), it was preferable to add more detailed docstring.
@apast apast changed the title Add CLI test coverage (pytest_watch.command: 98%) [WIP] Add CLI test coverage (pytest_watch.command: 98%) Jan 4, 2018
@apast apast changed the title [WIP] Add CLI test coverage (pytest_watch.command: 98%) Add CLI test coverage (pytest_watch.command: 98%) Jan 4, 2018
apast pushed a commit to apast/pytest-watch that referenced this pull request Jan 4, 2018
Messages formatting using .format() methods

Tests over error messages for invalid --spool arguments

Related to: joeyespo#79 (review)
@apast
Copy link
Author

apast commented Jan 4, 2018

Joe,

it seems excellent to add this requirements-test.txt file and declare it in setup.py file.

I will implement this suggestion and commit soon.

[]'s,
Andre

@apast
Copy link
Author

apast commented Jan 4, 2018

I added a version checker inside setup.py for python 2 compatibility.

I really don't know if it is the best pratice, or can we keep it this way for this moment. What do you think?

[]'s,

@apast apast changed the title Add CLI test coverage (pytest_watch.command: 98%) Add CLI test coverage over pytest_watch.command Jan 4, 2018
suggiro and others added 29 commits May 20, 2018 21:46
… into setup.py

extras_require was added to config

mock module conditional installation was moved to extras_require section, simplifying duplicated verification
…ttern.

Initial usage of conftest.py and fixtures
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

4 participants