The if statement documentation is defining else as optional element:
if_stmt ::= "if" expression ":" suite ( "elif" expression ":" suite )* ["else" ":" suite]
So the use of the if statement without else in the following function is perfectly correct:
1 def example_fct(return_foobar):
2 result = "foo"
3
4 if return_foobar:
5 result += "bar"
6
7 return result
I fully cover this function by two tests:
import unittest
from example_module import example_fct
class MyTestSuite(unittest.TestCase):
def test_foo(self):
self.assertEqual(example_fct(False), "foo")
def test_foo_bar(self):
self.assertEqual(example_fct(True), "foobar")
If I run the tests, the coverage is missing a branch (the missing else) and reports it as Missing Lines 4->7. I don't want to add a else: pass and test against a branch that's doing nothing. Also if I put # pragma: no cover in the line with the if statement, the lines 4/5 will not be included in the coverage.
So is there a proper way to deal with this issue, without using an else: pass or # pragma: no cover?
I know that I can do a function call in one line like:
boolVar and call_to_func()
to replace:
if boolVar:
call_to_func()
but this will not work with an assign statement.
Aucun commentaire:
Enregistrer un commentaire