More Unit Testing

Introduction

One thing we’ve learned through decades of software engineering is that developing correct software is difficult! The best way to ensure correctness is to test thoroughly while software is being developed. Unit testing is a framework that automates testing by providing a standard format for tests and an easy way to execute them.

Today we will practice writing unit tests for the dice.py module for PA1

Writing Tests

The file test_dice.py already contains a few reasonable tests for the roll_dice method. Try adding some tests for are_valid. Ideally, your tests should all pass when executed against your own dice.py file.  

Tests for Dice

    
import unittest
import dice

class TestDice(unittest.TestCase):

    def test_roll_dice_valid(self):
        """valid dice"""
        expected_dice = [4, 4, 1]
        roll = dice.roll_dice(3, 0)
        self.assertListEqual(expected_dice, roll)

    def test_roll_dice_too_many(self):
        """Invalid dice too many dice"""
        expected_dice = [6]
        roll = dice.roll_dice(14, 0)
        self.assertListEqual(expected_dice, roll)

if __name__ == '__main__':
    unittest.main()

 

Back to Top