Unittesting in a Jupyter notebook

Example - Unittesting

In this example I show how to run a unittest within your Jupyter Notebook with two simple classes.

Create two classes

Castle class

The important method for the castle is the access method. It will grant access when the character has the powerup 'Super Mushroom'.

In [1]:
class Castle(object):
    def __init__(self, name):
        self.name = name
        self._boss = 'Bowser'
        self._world = 'Grass Land'

    def access(self, character):
        if character.powerup == 'Super Mushroom':
            return True
        else:
            return False
    
    def get_boss(self):
        return self._boss

    def get_world(self):
        return self._world

Character class

By default the powerup of a character is empty.

In [2]:
class Character(object):
    def __init__(self, name):
        self.name = name
        self.powerup = ''

    def powerup(self, powerup):
        self.powerup = powerup
        
    def get_powerup(self):
        return self.powerup

Create a test class

We will create two characters and test that only the right powerup gives access to the castle.

In [5]:
import unittest

class CharacterTestClass(unittest.TestCase):
    """ Defines the tests for the Character class """
    def setUp(self):
        """ Set the castle for the test cases """
        self.castle = Castle('Bowsers Castle')
    
    def test_default_cannot_access(self):
        """ Default can not access """
        default = Character('Default')
        self.assertFalse(self.castle.access(default))
    
    def test_mario_cannot_access(self):
        """ Mario cannot access """
        mario = Character('Mario')
        mario.powerup = 'Starman'
        self.assertFalse(self.castle.access(mario))
    
    def test_peach_can_access(self):
        """ Peach can access """
        peach = Character('Peach')
        peach.powerup = 'Super Mushroom'
        self.assertTrue(self.castle.access(peach))

    def test_default_castle_boss(self):
        """ Verifty the default boss is Bowser """
        self.assertEqual(self.castle.get_boss(), "Bowser")

    def test_default_castle_world(self):
        """ Verify the default world is Grass Land """
        self.assertEqual(self.castle.get_world(), "Grass Land")

Run the test suite

In [6]:
import sys
suite = unittest.TestLoader().loadTestsFromTestCase(CharacterTestClass)
unittest.TextTestRunner(verbosity=4,stream=sys.stderr).run(suite)
test_default_cannot_access (__main__.CharacterTestClass)
Default can not access ... ok
test_default_castle_boss (__main__.CharacterTestClass)
Verifty the default boss is Bowser ... ok
test_default_castle_world (__main__.CharacterTestClass)
Verify the default world is Grass Land ... ok
test_mario_cannot_access (__main__.CharacterTestClass)
Mario cannot access ... ok
test_peach_can_access (__main__.CharacterTestClass)
Peach can access ... ok

----------------------------------------------------------------------
Ran 5 tests in 0.004s

OK
Out[6]:
<unittest.runner.TextTestResult run=5 errors=0 failures=0>