-2

Here is a test :

@skip("my test is skipped")
def test_coverage():
    my_function()

My question is simple :

If i run my coverage, will my_function will be covered or not (considering my test is skipped) ?

Thanks !

AntoineLB
  • 482
  • 3
  • 19

1 Answers1

2

Skipped tests are not executed. Code that is not executed is not covered, by definition.

Demo; given a coverage_demo module:

def foo():
    var = "This function has is covered"

def bar():
    var = "This function is not"

and a coverage_demo_tests.py file:

from unittest import TestCase, skip
import coverage_demo

class DemoTests(TestCase):
    def test_foo(self):
        coverage_demo.foo()

    @skip("No bar for us today")
    def test_bar(self):
        import coverage_demo
        coverage_demo.bar()

if __name__ == '__main__':
    import unittest
    unittest.main()

running this under coverage shows that line 5 in coverage_demo is not executed:

$ coverage run coverage_demo_tests.py
s.
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK (skipped=1)
$ coverage report --include=coverage_demo\.py -m
Name               Stmts   Miss  Cover   Missing
------------------------------------------------
coverage_demo.py       4      1    75%   5

The def statements at the top level are always executed, but line 5 is the only line in the bar() function.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343