Package trac :: Module test

Module test

source code

Classes
  MockPerm
Fake permission class.
  TestSetup
Test suite decorator that allows a fixture to be setup for a complete suite of test cases.
  TestCaseSetup
  InMemoryDatabase
DB-API connection object for an SQLite in-memory database, containing all the default Trac tables but no data.
  EnvironmentStub
A stub of the trac.env.Environment object for testing.
Functions
 
Mock(bases=(), *initargs, **kw)
Simple factory for dummy classes that can be used as replacement for the real implementation in tests.
source code
 
get_dburi() source code
 
reset_postgres_db(db, db_prop) source code
 
reset_mysql_db(db, db_prop) source code
 
locate(fn)
Locates a binary on the path.
source code
 
suite() source code
Variables
  INCLUDE_FUNCTIONAL_TESTS = True

Imports: doctest, os, unittest, sys, Locale, Configuration, Component, ComponentManager, Environment, DatabaseManager, SQLiteConnection, trac, load_workflow_config_snippet, translation


Function Details

Mock(bases=(), *initargs, **kw)

source code 

Simple factory for dummy classes that can be used as replacement for the real implementation in tests.

Base classes for the mock can be specified using the first parameter, which must be either a tuple of class objects or a single class object. If the bases parameter is omitted, the base class of the mock will be object.

So to create a mock that is derived from the builtin dict type, you can do:

>>> mock = Mock(dict)
>>> mock['foo'] = 'bar'
>>> mock['foo']
'bar'

Attributes of the class are provided by any additional keyword parameters.

>>> mock = Mock(foo='bar')
>>> mock.foo
'bar'

Objects produces by this function have the special feature of not requiring the 'self' parameter on methods, because you should keep data at the scope of the test function. So you can just do:

>>> mock = Mock(add=lambda x,y: x+y)
>>> mock.add(1, 1)
2

To access attributes from the mock object from inside a lambda function, just access the mock itself:

>>> mock = Mock(dict, do=lambda x: 'going to the %s' % mock[x])
>>> mock['foo'] = 'bar'
>>> mock.do('foo')
'going to the bar'

Because assignments or other types of statements don't work in lambda functions, assigning to a local variable from a mock function requires some extra work:

>>> myvar = [None]
>>> mock = Mock(set=lambda x: myvar.__setitem__(0, x))
>>> mock.set(1)
>>> myvar[0]
1

locate(fn)

source code 

Locates a binary on the path.

Returns the fully-qualified path, or None.