Lesson 11: Testing
Homepage | Content | Slides | Video |
Warning
This lesson is under construction. Learn from it at your own risk. If you have any feedback, please fill out our General Feedback Survey.
Homepage | Content | Slides | Video |
Warning
This lesson is under construction. Learn from it at your own risk. If you have any feedback, please fill out our General Feedback Survey.
def add_double(x, y):
return 2*(x+y)
def test_add_double():
expect(add_double(1, 2) == 6)
Most tests consist of the same general structure:
Simulating behavior external to a program so your tests can run independently of other platforms.
You're testing your program, not somebody else's. Mock other people's stuff, not your own.
$ run tests
Finding tests...
Running tests in tests/foo.ext
Running tests in tests/bar.ext
Running tests in misc/test_baz.ext
While you can write tests the hard way:
var = some_function(x)
if var == expected_output:
continue
else
print("Test X failed!")
$ run test
Test 5 failed!
It's usually easier to use a framework.
def simple_test():
expect(some_function(x), expected_output)
$ run tests
....x.....
Test 5 failed.
Debug information:
...
def tests_setup():
connect to database
populate database with test data
def tests_teardown():
delete all data from test database
disconnect from database
def some_test()
setup is called automatically
use data in database
assert something is true
teardown is run automatically
Let's suppose that we want to add a new view to the Flask app we created in the Frameworks lesson's TODO. When the user enters the url /hello/<name>, where "name" is any string of the user's choice, the view should return "Hello <name>!!" BEFORE you actually write this view, write a test that will test the desired functionality first-- i.e., test that your hello.py returns "Hello bob!!" when "bob" is provided as the name variable. AFTERWARDS, implement the actual view to make your test(s) pass.