r/Python • • Nov 14 '17

Senior Python Programmers, what tricks do you want to impart to us young guns?

Like basic looping, performance improvement, etc.

1.3k Upvotes

629 comments sorted by

View all comments

1

u/Manhigh Nov 14 '17

Test early, test often.

Use the unittest package to construct tests for your code. Run those tests often (automatic CI like travis or circleCI is great for this). Use something like nose or testflo to recursively run all tests in your package and report out coverage, so you can know which pieces of your code you haven't covered.

As someone else mentioned, a linter is a good idea. Try to follow PEP8 as much as possible (I know a lot of people are lax on the 79 character limit but you can have the checkers ignore that.

1

u/fiddle_n Nov 14 '17

Use the unittest package to construct tests for your code.

Actually I'd recommend pytest over unittest, unless the person is constrained to only use certain libraries.

1

u/Manhigh Nov 14 '17

I should have clarified. Use whatever you want to run the tests, but in my experience the tests seem to be defined as unittest.TestCase subclasses.

https://docs.pytest.org/en/latest/unittest.html

3

u/fiddle_n Nov 14 '17

I use pytest mostly because I don't want to use unittest nomenclature to write my tests. Writing tests in the pytest style is often more Pythonic. You are not forced to write classes for tests (you can just use top-level functions) and you use the built-in assert keyword rather than self.assertEquals.