Lin Du
Lin Du

Reputation: 102527

Run test and collect the coverage report using a single command

I am using Coverage.py package to collect the coverage report.

For now, I am collect the coverage report by running two commands like below:

venv) ☁  python-codelab [master] ⚡  coverage run /Users/ldu020/workspace/github.com/mrdulin/python-codelab/src/unittest/with-asyncio/test_person.py
test_create (__main__.TestPerson)
create person ... ok

----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
(venv) ☁  python-codelab [master] ⚡  coverage report                                                                                                
Name                                       Stmts   Miss  Cover
--------------------------------------------------------------
src/unittest/with-asyncio/person.py            7      0   100%
src/unittest/with-asyncio/test_person.py      21      0   100%
--------------------------------------------------------------
TOTAL                                         28      0   100%
  1. coverage run
  2. coverage report

As you can see, I ran two commands. I am looking for a way which is able to run the tests and collect the coverage report by a single command, not two. Some command like:

coverage run --coverage script.py

How to do that?

Upvotes: 1

Views: 985

Answers (1)

Lin Du
Lin Du

Reputation: 102527

The honor belongs to @MarekSchwarz.

Using coverage run script.py && coverage report is a way.

E.g.

(venv) ☁  python-codelab [master] ⚡  coverage run /Users/ldu020/workspace/github.com/mrdulin/python-codelab/src/unittest/with-asyncio/test_person.py && coverage report -m        
test_create (__main__.TestPerson)
create person ... ok

----------------------------------------------------------------------
Ran 1 test in 0.003s

OK
Name                                       Stmts   Miss  Cover   Missing
------------------------------------------------------------------------
src/unittest/with-asyncio/person.py            7      0   100%
src/unittest/with-asyncio/test_person.py      21      0   100%
------------------------------------------------------------------------
TOTAL                                         28      0   100%

Upvotes: 1

Related Questions