Giter Site home page Giter Site logo

gitter-badger / python_wrap_cases Goto Github PK

View Code? Open in Web Editor NEW

This project forked from erm0l0v/python_wrap_cases

0.0 1.0 0.0 135 KB

Simple library for generate test cases with parameters.

Home Page: http://python-wrap-cases.readthedocs.org

License: BSD 3-Clause "New" or "Revised" License

Makefile 6.11% Python 93.89%

python_wrap_cases's Introduction

Python wrap cases

image

image

Documentation Status

Simple library for generate test cases with parameters.

What is this?

This library helps to generate tests with parameters.

Let's write some tests for this function:

import re


def clear_start_end_dash(string):
    return re.sub(r'^[\s\-]*-|-[\s\-]*$', '', string)

We may write something like this:

from unittest import TestCase


class ClearStartEndDashTest(TestCase):

    def test_remove_first_dash(self):
        result = clear_start_end_dash('-my string')
        self.assertEqual(result, 'my string')

    def test_remove_all_first_dashes(self):
        result = clear_start_end_dash('-  -- --my string')
        self.assertEqual(result, 'my string')

    def test_remove_last_dash(self):
        result = clear_start_end_dash('my string-')
        self.assertEqual(result, 'my string')

    def test_remove_all_last_dashes(self):
        result = clear_start_end_dash('my string-- -- -- - ')
        self.assertEqual(result, 'my string')

    def test_keep_dash_at_center(self):
        result = clear_start_end_dash('my-string')
        self.assertEqual(result, 'my-string')

It's good, but we spent a lot of time to write those absolutely the same test functions.

So let's decrease the number of duplicate functions:

from unittest import TestCase


class ClearStartEndDashDryTest(TestCase):

    def test_remove_dash(self):
        cases = (
            ('-my string', 'my string'),
            ('-  -- --my string', 'my string'),
            ('my string-', 'my string'),
            ('my string-- -- -- - ', 'my string'),
            ('my-string', 'my-string')
        )
        for string, expected_result in cases:
            result = clear_start_end_dash(string)
            self.assertEqual(result, expected_result)

This code has a few problems:

  • Easy to write but difficult to read.
  • We can't use test fixture (setUp, tearDown) with each case.
  • If some case fails, the other cases won't run.
  • If test test_remove_dash fails, it won't help us find out what happened.

Look how easy we may solve these problems using this library:

from unittest import TestCase
from python_wrap_cases import wrap_case


@wrap_case
class ClearStartEndDashWrapTest(TestCase):

    @wrap_case('-my string', 'my string')
    @wrap_case('-  -- --my string', 'my string')
    @wrap_case('my string-', 'my string')
    @wrap_case('my string-- -- -- - ', 'my string')
    @wrap_case('my-string', 'my-string')
    def test_remove_dash(self, string, expected_result):
        result = clear_start_end_dash(string)
        self.assertEqual(result, expected_result)

This code generates 5 tests, that works like a simple test functions.

Console output:

test_remove_dash_u'-  -- --my string'_u'my string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok
test_remove_dash_u'-my string'_u'my string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok
test_remove_dash_u'my string-'_u'my string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok
test_remove_dash_u'my string-- -- -- - '_u'my string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok
test_remove_dash_u'my-string'_u'my-string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok

Installation

pip install python_wrap_cases

Free software: BSD license

Documentation: https://python_wrap_cases.readthedocs.org.

python_wrap_cases's People

Contributors

erm0l0v avatar

Watchers

The Gitter Badger avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.