Skip to main content

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.

Metadata

Release files for uspec 1.0.24

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for uspec 1.0.24
File Size Uploaded
uspec-1.0.24.tar.gz 7.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for uspec 1.0.24
File Interpreter ABI Platform
uspec-1.0.24-py3-none-any.whl Python 3 none any Details

Total release size: 16.1 kB

Release files / uspec-1.0.24.tar.gz

Download URL uspec-1.0.24.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
eb4d1a2ae97a1a37c181ad5a24dc8e5ecd79d96fb6506f8de2e78fc6fce37a84
BLAKE2b-256 checksum
How to use checksums
78ba6e34ef402c02dcca96cca16c6314113a167777122e94e51fa642211dee82
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / uspec-1.0.24-py3-none-any.whl

Download URL uspec-1.0.24-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9cde3f1f40921b3f08bd607efdec1c78b3bfe078b11f55d10c948c2ef5e516b3
BLAKE2b-256 checksum
How to use checksums
538f2dbe0ed023f76aefd1d299f4d5004fc8fe2909f42a82d6bb6f4aff88190c
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.0.24 This release

2 release files

1.0.23

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page