PySpec Usage Guide


Author Shibukawa Yoshiki
Contact yoshiki at
Copyright This document has been placed in the public domain.


Write and Run Specs

Writing Specs

In PySpec, the specification is represented in python function or method.
These specification methods have special decorator.


# This is function style spec

def New_list_should_be_empty():

# This is method style spec
class List_Behavior(object):
  def New_list_should_be_empty():

If you want to share any objects between specifications and contexts
(context is a setup method), you should write specs in class. PySpec
creates one object per one spec. It doesn't pollute other test environment.

PySpec searches specs from top level functions and classes. If you write
specs in nested class, that will not be called.

Running Specs on Console

You have two ways to run console runner. First way is script.
Another is writing startup code into your spec source files. It's easy.


from pyspec import *

# Define some specs here

if __name__ == "__main__":

You don't have to register your specs. PySpec searches all modules that are
loaded in your interpreter automatically. So, this code can kick specs in

If you want to run all specs that are divided into some files, creating
startup script is the easiest.


from pyspec import*

import behavior_entities
import behavior_database_access
import behavior_views


If you have created a spec startup script file and added startup code into all
your specs, you would be able to all tests and each test.

Running Specs on GUI

You can use GUI runner too. Run script. You can load spec module file
from [Project] - [Add module...] menu.
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
notice: Now I rebuilding GUI runner. There are some bugs and not fixed.</td>


If you want to run GUI startup code from your spec script, please add
following code:

if __name__ == "__main__":
    import pyspec.wxui.controller
    controller = pyspec.wxui.controller.WxPySpecController(0)

Spec Names

The spec which have good name is good spec. In pyspec, you have 2 ways to set the name.
These spec names will appear in the result report.

Method Name

Using method name is more simple way.
Method Name Spec Name
Astackwhichhaveone_value() A stack which have one value
versioninfoinsysmodule() versioninfo in sys module
oneoftestmethodshouldfail() one of testmethod should fail

This feature is implemented in pyspec.util.createspecname().


Docstring is used for the spec names. Python 2.X can accept ASCII names only as a method name.
This function is for programmers in Asia.

If you write docstring in context method and spec method, the first line of docstring
is used as a spec name.

PySpec gets docstring encoding from the first line of source code. If you want to use
Japanese or Chinese or any languages, you should write as following:

# -*- coding: utf8 -*-

Pyspec can accept any codec which is installed in python.


class StackBehavior(object):
    def A_stack_with_one_item(self):
        """Youso wo hitotsu motsu stack."""
        self.stack = Stack()
        self.stack.push("one item")

    def should_not_be_empty(self):
        """kara dewa nai."""


Youso wo hitotsu motsu stack
    kara dewa nai ... OK

This sample docstrings are Japanese pronunciation in English.
Of course, you can write real Japanese.

Last edited Feb 21, 2008 at 4:58 PM by shibu, version 2


No comments yet.