Discover this podcast and so much more

Podcasts are free to enjoy without a subscription. We also offer ebooks, audiobooks, and so much more for just $11.99/month.

147: Testing Single File Python Applications/Scripts with pytest and coverage

147: Testing Single File Python Applications/Scripts with pytest and coverage

FromTest and Code


147: Testing Single File Python Applications/Scripts with pytest and coverage

FromTest and Code

ratings:
Length:
12 minutes
Released:
Mar 5, 2021
Format:
Podcast episode

Description

Have you ever written a single file Python application or script?
Have you written tests for it?
Do you check code coverage?
This is the topic of this weeks episode, spurred on by a listener question.
The questions:
* For single file scripts, I'd like to have the test code included right there in the file. Can I do that with pytest?
* If I can, can I use code coverage on it?
The example code discussed in the episode: script.py
```
def foo():
return 5
def main():
x = foo()
print(x)
if name == 'main': # pragma: no cover
main()
test code
To test:
pip install pytest
pytest script.py
To test with coverage:
put this file (script.py) in a directory by itself, say foo
then from the parent directory of foo:
pip install pytest-cov
pytest --cov=foo foo/script.py
To show missing lines
pytest --cov=foo --cov-report=term-missing foo/script.py
def test_foo():
assert foo() == 5
def test_main(capsys):
main()
captured = capsys.readouterr()
assert captured.out == "5\n"
```
Released:
Mar 5, 2021
Format:
Podcast episode

Titles in the series (100)

Test & Code is a weekly podcast hosted by Brian Okken. The show covers a wide array of topics including software engineering, development, testing, Python programming, and many related topics. When we get into the implementation specifics, that's usually Python, such as Python packaging, tox, pytest, and unittest. However, well over half of the topics are language agnostic, such as data science, DevOps, TDD, public speaking, mentoring, feature testing, NoSQL databases, end to end testing, automation, continuous integration, development methods, Selenium, the testing pyramid, and DevOps.