I have a class, having 3 methods(in python) .
class MyClass:
def A(self):
.......
def B(self):
........
def C(self):
........
I have written unit test case for only one method A. This unit test covers each line of the method A. That is we don't have any if...else or any branching constructs.
What would be the code coverage percentage?
Again if I write another unit test case for 2nd method of the class covering all lines. What would be the code coverage percentage now?
I got the answer myself :-)
Code coverage is all depends on which module or which files you are running the coverage for. Lets say if we run coverage for one file the way i had framed my question. Each line in each method will be accounted for code coverage.
Now as per my question i am covering only one method contain 20 lines. Other 2 methods have another 80 lines (total 100 lines in 3 methods). So if i ran coverage for my file. I would get code coverage of only 20%.
In python we can run (in pycharm terminal) like :coverage run -m py.test my_file.py
To get the report run the command :coverage report -m py.test my_file.py
To run for entire module (in all packages) use : coverage run -m py.test and coverage report -m py.test
Related
I'm running a set of tests with pytest and want also to measure its coverage with the coverage package.
I have some decorators in my code, dividing the tests in several suites using something like:
#pytest.mark.firstgroup
def test_myfirsttest():
assert True is True
#pytest.mark.secondgroup
def test_mysecondtest():
assert False is False
I need to find something like:
coverage run --suite=firstgroup -m pytest
So that in this case only the first test, since it's in the correct suite, will be used to test coverage.
The suites work correctly when I use directly pytest but I don't know how to use them in coverage
Is there any way to configure coverage to achieve this?
You can pass whatever arguments you want to pytest. If this line works:
pytest --something --also=another 1 2 foo bar
then you can run it under coverage like this:
coverage run -m pytest --something --also=another 1 2 foo bar
I'm trying to get the executed code out of each Pytest Test case -in order to make a debuger-, now it's possible in Coverage module apis i will use the following code:
cov.start()
assert nrm.mySqrt(x) == 3 // this is a test case using Pytest
cov.stop()
cov.json_report()
cov.erase()// after this line i repeat the steps top
but it's not practical, so if I used the coverage run - m pytest fileName command it will not give me a report for each case, I'm trying to use the plugin in order to apply some special case where if the executed line is including assert I want to get a JSON report and the erase the data, will I be able to do that? or should I try to find a way to implant the code above for each test case?
I have a large python test file using unittest that I run from the command line. Some tests take a while to run. This is a mild pain point because I'm often only concerned with the last test I added. What I want is this:
add test.
run tests (one fails because I haven't written the code to make it pass)
implement the behaviour
run only the test that failed last time
fix the silly error I made when implementing the code
run only the failing test, which passes this time
run all the tests to find out what I broke.
Is it possible to do this from the command line?
(Not a fully automated solution, but better than the existing one)
If you pass the name of a test class as an argument to the test script, only that test will be run. For example, if you only want to run tests in the MyTest class in the script test_whatever.py:
python3 test_whatever.py MyTest
You can also specify an individual test as a member of that class. For example, suppose you want to run the test test_something in the class MyTest:
python3 test_whatever.py MyTest.test_something
Every test function is declared like:
def test_something_something(self):
If you add an underscore in front, like:
def _test_something_something(self):
that test will be ignored. One thing you can do is to do a quick find and replace in vim. Find all "test_"s and replace them with "_test_" and then find the one test that failed and remove the underscore.
Just run the test with --last-failed option (you might need pytest)
This is my tests class, in mymodule.foo:
class Some TestClass(TestCase):
def setUpClass(cls):
# Do the setup for my tests
def test_Something(self)
# Test something
def test_AnotherThing(self)
# Test another thing
def test_DifferentStuff(self)
# Test another thing
I'm running the tests from Python with the following lines:
tests_to_run = ['mymodule.foo:test_AnotherThing', 'mymodule.foo:test_DifferentStuff']
result = nose.run(defaultTest= tests_to_run)
(This is obviously a bit more complicated and there's some logic to pick what tests I want to run)
Nose will run just the selected tests, as expected, but the setUpClass will run once for every test in tests_to_run. Is there any way to avoid this?
What I'm trying to achieve is to be able to run some dynamic set of tests while using nose in a Python script (not from the command line)
As #jonrsharpe mentioned, setupModule is what I was after: it will run just once per the whole module where my tests reside.
I am trying to achieve a 100% coverage for a basic python module.
I use Ned Batchelder's coverage.py module to test it.
1 class account(object):
2 def __init__(self, initial_balance=0):
3 self.balance = initial_balance
4 def add_one(self):
5 self.balance = self.balance + 1
These are the tests.
class TestAccount(unittest.TestCase):
def test_create_edit_account(self):
a = account1.account()
a.add_one()
Here is what the coverage report I get.
COVERAGE REPORT =
Name Stmts Miss Cover Missing
-----------------------------------------------------
__init__ 1 1 0% 1
account1 5 3 40% 1-2, 4
account2 7 7 0% 1-7
As we can see, the lines 1-2 and 4 are not covered which are the defintions.
The rest of the lines are executed.
I think your problem is described in the FAQ:
Q: Why do the bodies of functions (or classes) show as executed, but
the def lines do not?
This happens because coverage is started after the functions are
defined. The definition lines are executed without coverage
measurement, then coverage is started, then the function is called.
This means the body is measured, but the definition of the function
itself is not.
To fix this, start coverage earlier. If you use the command line to
run your program with coverage, then your entire program will be
monitored. If you are using the API, you need to call coverage.start()
before importing the modules that define your functions.
Following jcollado's answer:
I have this problem with Django nose which only covers lines used by tests.
For fix it I launch firstly manage.py with coverage and after I launch tests.
.coverage file will contain both's reports.
My first command is a custom which prints my project settings. Example:
coverage run ./manage.py settings && ./manage.py test myapp