Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Minimalistic BDD in Python (ah, my first forray into the wonderful world of test runners!)

License

NotificationsYou must be signed in to change notification settings

mdw-archives/pyspecs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

pyspecs is a testing framework that strives to achieve more readablespecifications (tests) by leveraging some fancy syntactic sugar andauto-discovery of tests/specs. WARNING: version 2.0 introduces breakingchanges if you've been using 1.0 or 1.1.

Installation is straightforward:

$ pip install pyspecs

or...

$ easy_install pyspecs

or...

$ git clone https://mdwhatcott@github.com/mdwhatcott/pyspecs.git$ cd pyspecs$ python setup.py

Assertions

The main tool for verifying behavior is an assertion of some kind. Thesimplest assertion can be made by using the built-in assert statement:

assert 42 == 'The answer the life, the universe and everything'

For readability this project provides a more fluent method for makingassertions:

# These imported names are all synonyms for the class that# provides fluent assertions (Should). Use whichever provides# the best readability.  The general patter is:# >>> the([value]).should.[condition_method]([comparison_args])#  or...# >>> the([value]).should_NOT.[condition_method]([comparison_args]) # negated!from pyspecs import the, this, that, itthis(42).should.equal(42) # this passesthis([1, 2, 3]).should.contain(2) # this also passesthe(list()).should.be_empty() # passesit(1).should_NOT.be_greater_than(100) # passes# raises AssertionError, caught by framework, logged as failurethat(200).should.be_less_than(0)

Writing complete specs

from pyspecs import given, when, then, and_, thewith given.two_operands:    a = 2    b = 3    with when.supplied_to_the_add_function:        total = a + b        with then.the_total_should_be_mathmatically_correct:            the(total).should.equal(5)        with and_.the_total_should_be_greater_than_either_operand:            the(total).should.be_greater_than(a)            the(total).should.be_greater_than(b)    with when.supplied_to_the_subtract_function:        difference = b - a        with then.the_difference_should_be_mathmatically_correct:            the(difference).should.equal(1)    # cleanup is just based on scope    del a, b, total, difference

Notice that the names of each step are supplied as dynamic attributes of thegiven,when,then, andand_ step keywords. These user-created attributesare used in the output (below). Here is a listing of words that can beused as steps:

  • given
  • provided
  • when
  • then
  • and_
  • so
  • therefore
  • however
  • as_well_as

These steps can be arranged in any order and hierarchy for compose aspecification (spec). You can even create your own steps that suit your needs(see the source code for how that's done).

Execution of specs

Beyond providing the python library which will be explained below, installationprovides a command-line script into the environment, meant to be invokedfrom the root of your project. The script will execute all specs in .py filesending in 'test.py' or 'tests.py' or beginning with 'test'.

To run all tests once:

$ run_pyspecs.py

To begin an auto-test loop (runs all specs anytime a .py file is saved):

$ run_pyspecs.py -w

Complete Example

There are some complete examples of specs, code, and output in theexamples folder.

About

Minimalistic BDD in Python (ah, my first forray into the wonderful world of test runners!)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp