Beginners Python Cheat Sheet Pcc Testing
Beginners Python Cheat Sheet Pcc Testing
Cheat Sheet - TestCase and write methods that begin with test_. Save this as
test_full_names.py
import unittest
tests. This will tell you whether the changes you made affected
existing behavior.
E
class NamesTestCase(unittest.TestCase):
================================================
ERROR: test_first_last (__main__.NamesTestCase)
Test names like Janis Joplin.
"""Tests for names.py.""" ------------------------------------------------
Why test your code? Traceback (most recent call last):
def test_first_last(self): File "test_full_names.py", line 10,
When you write a function or a class, you can also """Test names like Janis Joplin.""" in test_first_last
write tests for that code. Testing proves that your code full_name = get_full_name('janis', 'joplin')
works as it's supposed to in the situations it's designed 'joplin') TypeError: get_full_name() missing 1 required
to handle, and also when people use your programs in self.assertEqual(full_name, positional argument: 'last'
unexpected ways. Writing tests gives you confidence 'Janis Joplin')
that your code will work correctly as more people ------------------------------------------------
if __name__ == '__main__': Ran 1 test in 0.001s
begin to use your programs. You can also add new unittest.main()
features to your programs and know whether or not FAILED (errors=1)
you've broken existing behavior by running your tests. Running the test
A unit test verifies that one specific aspect of your Python reports on each unit test in the test case. The dot represents Fixing the code
a single passing test. Python informs us that it ran 1 test in less than When a test fails, the code needs to be modified until the test
code works as it's supposed to. A test case is a 0.001 seconds, and the OK lets us know that all unit tests in the test passes again. Don’t make the mistake of rewriting your tests to fit
collection of unit tests which verify that your code's case passed. your new code, otherwise your code will break for anyone who's
behavior is correct in a wide variety of situations. .
using it the same way it's being used in the failing test. Here we can
make the middle name optional.
---------------------------------------
Testing a function: a passing test Ran 1 test in 0.000s def get_full_name(first, last, middle=''):
Python's unittest module provides tools for testing your """Return a full name."""
code. To try it out, we’ll create a function that returns a full OK
name. We’ll use the function in a regular program, and then if middle:
build a test case for the function. full_name = f"{first} {middle} {last}"
Testing a function: A failing test else:
A function to test Failing tests are important; they tell you that a change in the full_name = f"{first} {last}"
Save this as full_names.py code has affected existing behavior. When a test fails, you
def get_full_name(first, last): need to modify the code so the existing behavior still works. return full_name.title()
"""Return a full name.""" Modifying the function Running the test
We’ll modify get_full_name() so it handles middle names, but Now the test should pass again, which means our original
full_name = f"{first} {last}" we’ll do it in a way that breaks existing behavior. functionality is still intact.
return full_name.title()
def get_full_name(first, middle, last): .
Using the function """Return a full name.""" ---------------------------------------
Save this as names.py full_name = f"{first} {middle} {last}" Ran 1 test in 0.000s
return full_name.title()
from full_names import get_full_name
Using the function OK
janis = get_full_name('janis', 'joplin')
print(janis) from full_names import get_full_name