Skip to main content

RSpec like behavior driven development (BDD) tool based on unittest

Project description

USpec

USpec is a 'Domain Specific Language' (DSL) testing tool written in Python to test Python code in a 'Behavior-Driven Development' (BDD) mannar.

  • Alomost the same grammar as Rspec
  • Alternative representaiotn of a test case of unittest. Since Uspec file can be tested by unittest, Developers can gradually migrate existing unittests to USpec.

What is USpec?

USpec has the alomost the same grammar as RSpec

Let's assume that Bowling class calculates the sum of game scores if there is no strikes and spares.

  • bowling.py

    class Bowling(object):
        
        def __init__(self):
            self._score = 0
        
        def hit(self, pins):
            self._score += pins
    
        def score(self):
            return self._score
    

For example, the instance of the Bowling class returns 80 for 20 same hit numbers 4, 4, ... , 4. This example can be written in bowling_spec.rb by Rspec file which is the most common BDD tool. Using USpec, we can write a spec Python file bowling_spec.py equivalent to bowling_spec.rb

  • bowling_spec.rb

     RSpec.describe Bowling, "#score" do
         context "with no strikes or spares" do
             it "sums the pin count for each roll" do
                 bowling = Bowling.new
                 20.times { bowling.hit(4) }
                 expect(bowling.score).to eq 80
             end
         end
     end
    
  • bowling_spec.py

     from uspec import description, context, it
     
     with description(Bowling, "#score"):
         with context("with no strikes or spaces"):
             @it("sums the pin count for each roll")
             def _(self):
                 bowling = Bowling()
                 for i in range(20): bowling.hit(4)
                 self.assertEqual(bowling.score(), 80)
    

Uspec file is an alternative representation of unittest file

When a Uspec file is loaded by Python runtime, it generates a sub class of unittest.TestCase in that place.

  • The equivalent test case of the USpec file bowling_spec.py above benerated by USpec is:

     import unittest
     class TestBowling_Score__WithNoStrikesOrSpaces(unittest.TestCase):
         def test_sums_the_pin_count_for_each_roll(self):
             bowling = Bowling()
             for i in range(20):
                 bowling.hit(4)
             self.assertEqual(bowling.score(), 80)
    

Uspec file can be tested by unittest

  • Uspec file behaves as if it was written in unittest format, so it can be tested by unittest below:

     $ python -m unittest -v bowling_spec.py 
     test0000: Bowling#score (with no strikes or spaces) sums the pin count for each roll (uspec.TestBowling#score) ... ok
     
     ----------------------------------------------------------------------
     Ran 1 tests in 0.000s
     
     OK
    
  • Mix of Uspec files and unittest files can be also tested by unittest. Assume that test_baseball.py is the traditional unittest file. Since bowling_spec.py and test_baseball.py can be processed simultaneously by unittest, The total count of test cases becomes sum of test case of these files:

     $ python -m unittest -v bowling_spec.py   test_baseball.py
     test0000: Bowling#score (with no strikes or spaces) sums the pin count for each roll (uspec.TestBowling#score) ... ok
     test_game_countes (test_baseball.py.TestBaseball) ... ok
     
     ----------------------------------------------------------------------
     Ran 2 tests in 0.000s
     
     OK
    

Usage

Author

License

This project is licensed under the MIT License - see the LICENSE file for details

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

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

uspec-1.0.24.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

uspec-1.0.24-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file uspec-1.0.24.tar.gz.

File metadata

  • Download URL: uspec-1.0.24.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for uspec-1.0.24.tar.gz
Algorithm Hash digest
SHA256 eb4d1a2ae97a1a37c181ad5a24dc8e5ecd79d96fb6506f8de2e78fc6fce37a84
MD5 429d7f54e518b6dbd6026a651e092b02
BLAKE2b-256 78ba6e34ef402c02dcca96cca16c6314113a167777122e94e51fa642211dee82

See more details on using hashes here.

File details

Details for the file uspec-1.0.24-py3-none-any.whl.

File metadata

  • Download URL: uspec-1.0.24-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for uspec-1.0.24-py3-none-any.whl
Algorithm Hash digest
SHA256 9cde3f1f40921b3f08bd607efdec1c78b3bfe078b11f55d10c948c2ef5e516b3
MD5 20b2cee65780948e5f720f21b9832a44
BLAKE2b-256 538f2dbe0ed023f76aefd1d299f4d5004fc8fe2909f42a82d6bb6f4aff88190c

See more details on using hashes here.

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