JJ_29
JJ_29

Reputation: 53

code coverage of a file using coverage.py using a python script

I want to get the code coverage of a python file say "Test.py". Now, when I write the script for this using coverage.py, it looks like this,

import coverage

cov = coverage.Coverage()
cov.start()

#pass the data

cov.stop()
cov.save()
cov.html_report()

now in place of pass the data, I want to pass Test.py, How can I do this?

Upvotes: 1

Views: 1906

Answers (2)

Freddy
Freddy

Reputation: 879

I just have the same doubt.

This gave me the result you want using pytest

import coverage
import pytest

cov = coverage.Coverage()
cov.start()

pytest.main(['-x', 'api/tests.py', '-vv'])

cov.stop()
cov.report()

enter image description here

Upvotes: 2

Ned Batchelder
Ned Batchelder

Reputation: 375574

You don't need to use the coverage API. Just run your program with coverage:

$ coverage run Test.py

Upvotes: 0

Related Questions