Skip to main content

Generator is a helper for generating test methods for nose while still using unittest

Project description

https://img.shields.io/travis/kevinastone/generator.svg https://img.shields.io/pypi/v/generator.svg

Generator is a helper for generating test methods for nose while still using unittest.

Installation

pip install test-generator

Introduction

Have you ever written tests that loop through a list of inputs to validate the functionality?

Something like?

from mything import thingy

class MyTestCase(unittest.TestCase):
    def test_thingy(self):
        for input in [
            'a',
            'b',
            'cccc',
            'ddd'
            'eeeeee',
            'f',
            'g'
        ]:
            self.assertTrue(thingy(input))

But running in a loop limits all the functionality in TestCase like per- test setUp or tearDown. It also fails on the first input and you can’t run a single test input, you have to run them all? (Doesn’t work well when each test is more complicated than this toy case).

Instead, what if you wrote your test like:

from generator import generator, generate
from mything import thingy

@generator
class MyTestCase(unittest.TestCase):

    @generate('a', 'b', 'cccc', 'ddd', 'eeeeee', 'f', 'g')
    def test_thingy(self, input):
        self.assertTrue(thingy(input))

And when you run your tests, you see:

----------------------------------------------------------------------
Ran 7 tests in 0.001s

OK

Generator gives you simple decorators to mulitply your test methods based on an argument list. It’s great for checking a range of inputs, a list of error conditions or expected status codes.

Examples

API Client Error Handling

Let’s make sure our API client properly handles error conditions and raises a generic APIError under the conditions. We’ll use mock to patch out the actual API call to return our response.

import mock
from generator import generator, generate
from example import client, APIError

@generator
class TestAPIErrorHandling(unittest.TestCase):

    @generate(400, 401, 403, 404, 500, 502, 503)
    def test_error(self, status_code):
        with mock.patch(client, '_request') as _request_stub:
            _request_stub.return_value.status_code = status_code

            self.assertRaises(APIError):
                client.get('/path/')

Test Fixtures

Let’s make sure our API client properly handles error conditions and raises a generic APIError under the conditions. We’ll use mock to patch out the actual API call to return our response.

from generator import generator, generate
from example.sanitize import strip_tags

@generator
class TestStripTags(unittest.TestCase):

    @generate(
        ('<h1>hi</h1>', 'hi'),
        ('<script></script>something', 'something'),
        ('<div class="important"><p>some text</p></div>', 'some text'),
    )
    def test_strip_tags(self, input, expected):
        self.assertEqual(strip_tags(input), expected)

History

0.1.1 (2015-10-15)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

test-generator-0.1.2.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

test_generator-0.1.2-py2.py3-none-any.whl (6.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file test-generator-0.1.2.tar.gz.

File metadata

File hashes

Hashes for test-generator-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ad5925c814bfe79497b43df096e3bb52c166d1577f7aff160137301676232f4a
MD5 6c69e73ba5b4b3ed62f7bcda071c64f1
BLAKE2b-256 1eb390a71f2f4f9de5467c5518f0d75876eb7501c07fa1e25353ceaa56da3973

See more details on using hashes here.

File details

Details for the file test_generator-0.1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for test_generator-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7de659848ca3fafd6b64b802d06872d7c5eaa3835818083c4eb9c142e90fd904
MD5 b2075e2d02536088d57e806ecd42d7cc
BLAKE2b-256 e31c6cf39cf103e6a6f5b236a882c6da38688af58da1e5cdb25afa18ea12683a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page