PySpec is a behavior-driven development library in the spirit of RSpec. It enables you to test your code by specifying expected outcomes in prose-like language:
mug = Mug()
with description(Mug):
with description('.fill')
with context('coffee'):
with specification('fills the mug with coffee'):
mug.fill('coffee')
expect(mug.contents).to(eq('coffee'))
Expectations are a way to defined expected outcomes using examples. They may be used outside of PySpec by importing pyspec.expections.
The basic usage pattern of PySpec Expectations is:
expect(actual).to(matcher(expected))
expect(actual).not_to(matcher(expected))
Matchers are objects that have the following methods:
PySpec Expectations comes with several matchers for testing common outcomes.
Passes if actual > expected:
expected(actual).to(be_gt(expected))
Passes if actual < expected:
expect(actual).to(be_lt(expected))
Passes if actual >= expected:
expect(actual).to(be_gte(expected))
Passes if actual <= expected:
expect(actual).to(be_lte(expected))
Passes if (actual <= expected + delta) and (actual >= expected - delta):
expect(actual).to(be_within(delta).of(expected))
Passes if re.match(pattern, actual) is not None:
expect(actual).to(match(pattern))
Passes if isinstance(actual, expected):
expect(actual).to(be_an_instance_of(expected))
Passes if type(actual) is expected:
expect(actual).to(be_of_type(expected))
Passes if bool(actual):
expect(actual).to(be_truthy())
Passes if not bool(actual):
expect(actual).to(be_falsy())
Passes if calling actual raises expected:
expect(callable_actual[, *arguments[, **kwargs]]).to(raise_error(expected))
Represents a failed expectation.
Used to resolve match of actual against a matcher and propogate a failure if it does.
Used to resolve match of actual against matcher and propogate a failure if it does not.
Represents a value against which expectations may be tested.
Tests that actual is an instance of expected or a descendant thereof
Tests that actual is greater than or equal to expected
Tests that actual is specifically an instance of expected