-
Notifications
You must be signed in to change notification settings - Fork 28
Home
pyVows is the python version of the Vows.JS testing framework.
It allows for parallel execution of topic-oriented vows. Big words, huh?
It's pretty simple. Imagine we are testing a method that sums up two integers, like this:
def test_sum_returns_42():
result = add_two_numbers(41, 1)
assert result
assert int(result)
assert result == 42
Even in a VERY simple scenario like this, we have three assertions in this test. Not too good if we want a single assertion per test, so we could do it like this:
def test_sum_returns_result():
result = add_two_numbers(41, 1)
assert result
def test_sum_returns_a_number():
result = add_two_numbers(41, 1)
assert int(result)
def test_sum_returns_42():
result = add_two_numbers(41, 1)
assert result == 42
This is all fine and dandy, except that we are executing the add_two_numbers function three times. In this simple scenario we don't really care if it gets executed many times, but with real code we want to minimize calls so our tests are always as fast as possible.
This is how the above test would look like in pyVows:
class SumContext(Vows.Context):
def topic(self):
return add_two_numbers(41, 1)
def we_get_a_result(self, topic):
Vows.Assert.is_not_null(topic)
def we_get_a_number(self, topic):
Vows.Assert.is_numeric(topic)
def we_get_42(self, topic):
Vows.Assert.are_equal(42, topic)
Some things to notice here:
- Contexts are nestable, meaning you just have to create a new class inside that inherits from Vows.Context;
- Each context is executed in parallel to other contexts;
- The topic function gets executed only once and it's value is passed to all the other methods in the Context class;
- Each function other than topic is called a vow and is executed in parallel.
You can find more info about writing vows in Writing Vows and Honoring Vows.
Installing vows is as easy as pip install pyvows
.
Running vows (after it has been installed in the above step) is as easy as pyvows vows/
. To check the arguments available to pyvows, type pyvows --help
.