Skip to main content

Parametrized tests support for python builtin unitest framework

Project description

https://travis-ci.org/canni/ptestcase.svg?branch=master https://coveralls.io/repos/canni/ptestcase/badge.png?branch=master

PTestCase

Isolated test cases generated from a template method and set of unnamed or named calls provided in decorator fashion.

Changelog

  • 1.0.1: Maintenance release, no BC breaks

  • 1.0.0: Initial release

Installation

Supports Python >= 2.6, Python 3 * and PyPy

pip install ptestcase or easy_install ptestcase

* Although this works perfectly on Python 3, when yours code is targeting Python 3 only, you may use unittest module subtests feature. If you need to support both Pythons from the same codebase, it’s still the way to go.

The Problem:

How to write test cases for this simple function without much boilerplate code?

def example(value, transform='lower'):
    if transform == 'lower:
        return value.lower()
    elif transform == 'upper':
        return value.upper()
    else:
        return value

Test cases using PTestCase:

Every call definition passed to parameterize will be converted to a test method, and original template will be removed. Each generated test method will be executed in isolation, by the test runner.

from ptestcase import ParametrizedTestCase, call, parametrize

class ExampleTestCase(ParametrizedTestCase):
    @parametrize(
        call('TEST', 'test', transform='lower'),
        call('test', 'TEST', transform='upper'),
        no_transform=call('test', 'test', None)
    )
    def test_template(self, given, expected, transform):
        result = example(given, transform)

        self.assertEqual(result, expected)

    def test_exception_on_invalid_value(self):
        with self.assertRises(AttributeError):
            example(None)

Example output:

running test
test_template_case_1 (testpackage.ExampleTestCase) ... ok
test_template_case_2 (testpackage.ExampleTestCase) ... ok
test_template_no_transform (testpackage.ExampleTestCase) ... ok
test_exception_on_invalid_input (testpackage.ExampleTestCase) ... ok

Project details


Download files

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

Source Distributions

PTestCase-1.0.1.zip (6.8 kB view hashes)

Uploaded Source

PTestCase-1.0.1.tar.gz (4.0 kB view hashes)

Uploaded Source

Supported by

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