Disclaimer; beginner question!
My project structure, highly simplified for sake of the question, looks like this:
Project/
|-- main.py
|-- test_main.py
After reading Jeff Knupp's blogpost on unit testing and writing an assortment of tests I wanted to see how much of my code was now covered by tests. So I installed coverage.py and the following confuses me:
$ coverage run main.py (shows me the prints/logging from the script)
$ coverage report main.py
Name, Stmts, Miss, Cover
main.py, 114, 28, 75%
The thing is, I don't run unit tests from within the main script, nor did I think I should. I manually run all tests from test_main.py before a commit and know for a fact they do not cover 75% of all my statements. After reading coverage documentation I am doubting my unit test implementation ... do I need triggers from the main.py that run tests?
So I tried the same on my test script:
$ coverage run test_main.py (shows me an 'OK' test run for all tests)
$ coverage report test_main.py
Name, Stmts, Miss, Cover
test_main.py, 8, 0, 100%
But this is simply showing me I've "hit" 100% of my code in the test statements during execution of that script. So why then is coverage listed under "increase test coverage" if it simply displays what code has been used.
I would really like to see how much of my main.py is covered by test_main.py and am pretty sure I am missing some basic concept. Could someone elaborate?
On my Ubuntu machine running "coverage run test_main.py; coverage report" only gives me a report on test_main.py. On my Windows machine this gives:
Name, Stmts, Miss, Cover
main.py, 114, 74, 35%
test_main.py, 8, 0, 100%
TOTAL, 122, 74, 39%
The coverage report still doesn't make sense:
- The test_main covers 9 out of 134 lines of code and 1 out of 10 functions in main - coverage is not 35%
- Why is it reporting the coverage of test_main, these are the tests and it would be weird if this wasn't 100% since I'm running all tests to see my coverage ...
- I am doing something wrong here or this way of looking at it is bollocks, calculating an average of "coverage" while summing the tests with the code itself offers no insight and in my beginner opinion is wrong