0

Say I have method that just appends a string to an input string..

def request(
    url: str,
    headers: dict = None
):
    default_headers = {
        "user-agent": "some user agent"
    }
    
    if isinstance(headers, dict):
        default_headers.update(headers)

When I run a coverage report for this method, it says that default_headers.update(headers) needs coverage. How do I add coverage for this statement?

4
  • Why are you splitting apart your string and then putting it together again? ''.join([char for char in 'test']) is the same thing as 'test'. Also, you're using PEP 484 type hints, so I assume you're using mypy or some other type checker. That means you needn't be testing the return types of functions, since your type checker is doing that for you already. You should be testing that the values are correct. Commented Jun 13, 2022 at 3:02
  • probably something about the looping constructs -- like maybe an empty array will cover it Commented Jun 13, 2022 at 3:04
  • @SilvioMayolo point taken - i updated my question with an example method closer to my scenario.. Commented Jun 13, 2022 at 3:07
  • You can create a test case in which you pass headers(as dict) unless you haven't already being doing that(asking as we dont see any test case here). Commented Jun 13, 2022 at 4:10

1 Answer 1

1

Coverage counts lines which are actually encountered during execution in tests. So in your case, if that line is not being covered, it is because the conditional:

if isinstance(headers, dict):

must never evaluate to true in your test cases. To gain coverage for this line, pass a keyword argument headers as a dict.

A good discussion of statement coverage testing and its limitations can be found in this post: https://nedbatchelder.com/blog/200710/flaws_in_coverage_measurement.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.