Unittest python
Unittest python
In Debug > Settings Wheel > Configurations you can review your
config and edit the launch.json file. For example add arguments.
To make other essential adjustments search for: vscode python ”Open Settings
(JSON)” and your topic, program language etc.
More info at: https://code.visualstudio.com/docs/getstarted/tips-and-tricks
Python Assert statements
• Assertions are statements that assert or
state a fact confidently in your program
• Assertions are simply boolean
expressions that checks if the
conditions return true or false
• If it is true, the program does nothing
and move to the next line of code. If it's
false, the program stops and throws an
error
• It is also a debugging tool as it brings
the program to halt as soon as any error
have occurred and shows the location
where in the program the error occurred
Python assert statement example
• The condition is supposed to be always true. If the condition is false assert halts
the program and gives an AssertionError: assert <condition>, <error_message>
def calc_factorial(num): def test_negative_numbers_return_false():
if isinstance(num, str): # type(num) is str: checkcalc_factorial(-1, False, "test_negative_numbers_return_false")
print("Sorry, factorial does not exist for string input") def test_non_integer_return_false():
return False checkcalc_factorial(0.5, False, "test_non_integer_return_false")
elif num < 0: def test_when_input_is_zero_return_one():
print("Sorry, factorial does not exist for negative numbers") checkcalc_factorial(0, 1, "test_when_input_is_zero_return_one")
return False def test_when_input_is_three_teturn_six():
elif int(num) != num: checkcalc_factorial(3, 6, "test_when_input_is_three_return_six")
print("Sorry, factorial does not exist for real numbers") def test_string_input_return_false():
return False checkcalc_factorial('t', False, "test_string_input_return_false")
elif num == 0: if __name__ == '__main__':
print("The factorial of 0 is 1") try:
return 1 """
else: # change the value for a different result
factorial = 1 num = 7
for i in range(1, num + 1): # uncomment to take input from the user
factorial = factorial * i num = int(input("Enter a number: "))
print("The factorial of", num ,"is", factorial) calc_factorial(num):
return factorial """
https://en.wikipedia.org/wiki/Code_coverage
https://en.wikipedia.org/wiki/Control-flow_graph
Coverage.py
• Coverage.py is a tool for measuring code
coverage of Python programs. It monitors your
program, noting which parts of the code have
been executed, then analyzes the source to
identify code that could have been executed but
was not
• Note that high coverage numbers does not mean
that your code is clean from bugs!
# install
$ pip install coverage
$ coverage help
usage: coverage <command> [options] [args]
# run your test code (.coverage is created) PS C:\python_unittesting> coverage report -m
$ coverage run --branch test_factorial.py Name Stmts Miss Branch BrPart Cover Missing