Lesson 11: Testing

Homepage Content Slides Video


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)

Why Testing Matters

mars testing example

Structure of a Test

Most tests consist of the same general structure:

Types of Testing

Concept: Mocking

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.

Testing Frameworks

$ run tests
Finding tests...
Running tests in tests/foo.ext
Running tests in tests/bar.ext
Running tests in misc/test_baz.ext

Frameworks vs 'The Hard Way'

While you can write tests the hard way:

var = some_function(x)
if var == expected_output:
    print("Test X failed!")
$ run test
Test 5 failed!

Frameworks vs 'The Hard Way'

It's usually easier to use a framework.

def simple_test():
    expect(some_function(x), expected_output)
$ run tests
Test 5 failed.
Debug information:

Teardown and Setup

Useful for:
  • populating a test database
  • writing and deleting files
  • or anything else you want!
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

TODO: Using Python's unittest

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.

Unittesting in Flask
Check out the official Flask docs for help with syntax.

Further Reading

CS 362
This OSU Course covers testing very in depth and even covers types of testing including Random testing and testing analysis.
Python Unittest Documentation
A good reference for using Python's built-in unit-testing module.