Tagged in

Programming

New Light Technologies, Inc. (NLT
New Light Technologies, Inc. (NLT
New Light Technologies (NLT) provides enterprise information solutions that combine Information Technology (IT) ingenuity and industry intelligence to drive high-performance results for our clients. We help commercial companies and governments at the federal, state, and local
More information
Followers
23
Elsewhere
More, on Medium

More unittest Features

Last time, we moved the tests for our test-driven factorial function from basic assert statements to being run by Python’s unittest module:

import unittest
from factorial import factorial
class TestFactorial(unittest.TestCase):

Beyond assert

Last time, we built a factorial function in a test-driven style using Python’s assert statement:

def factorial(n):
if n < 0:
raise ValueError('Factorial of negative is undefined')
return n * factorial(n-1) if n else 1

Basic Negative Testing

In part one, we started writing a factorial function in the test-driven style. We depend on the humble “assert” statement to run our tests and we ended with this:

def factorial(n):
return n * factorial(n-1) if n else 1