QuickCheck

qc is a Python implementation of QuickCheck library for Haskell. Its purpose is to generate input data for tests. So instead of writing multiple tests to test arbitrary values or writing data generation logic inside of the test, developer can decorate test case and qc takes care of the rest.

Following example has test case called test_simple_move, which is just testing that taking a step to a random direction actually moves the character to desired location. @forall(tries=5, direction=integers(low = 1, high = 8)) decorator before the function is used to run the test five times, with direction randomly selected between 1 and 8 (inclusive).

@forall(tries=5, direction=integers(low = 1, high = 8))
def test_simple_move(self, direction):
    """
    Test that taking single step is possible
    """
    self.character.location = (5, 5)
    expected_location = [(0, 0),
                         (5, 4), (6, 4), (6, 5), (6, 6),
                         (5, 6), (4, 6), (4, 5), (4, 4)]
                 
    self.character.move(direction,
                        self.action_factory)
    
    assert_that(self.character.location,
                is_(equal_to(expected_location[direction])))

It’s worth noting, since the @forall decorator is between function and test framework (nose in my case), setup function is not automatically run between tests. From the point of view of nose, this is just a single test.

This is pretty nifty tool for testing. More than randomly generated integers are supported too, of course. Even creation of user defined objects is supported.

There is also work in progress version for .NET, that I might find useful in the future.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s