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
  EnvironmentStub
A stub of the trac.env.Environment class 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
 
MockRequest(env, **kwargs)
Request object for testing.
source code
 
get_dburi() source code
 
mkdtemp()
Create a temp directory with prefix trac-tempenv and return the directory name.
source code
 
locate(fn)
Locates a binary on the path.
source code
 
rmtree(path) source code
 
test_suite() source code
Variables
  locale_en = Locale('en', territory='US')
  INCLUDE_FUNCTIONAL_TESTS = True
  __package__ = 'trac'

Imports: abc, doctest, importlib, inspect, io, logging, numbers, os, shutil, sys, tempfile, time, types, unittest, Locale, trac, Configuration, ComponentManager, ComponentMeta, TracError, DatabaseManager, parse_connection_uri, Environment, PermissionCache, load_workflow_config_snippet, translation, time_now, utc, Request, arg_list_to_args, Chrome, Session


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

MockRequest(env, **kwargs)

source code 

Request object for testing. Keyword arguments populate an environ dictionary and the callbacks.

If authname is specified in a keyword arguments a PermissionCache object is created, otherwise if authname is not specified or is None a MockPerm object is used and the authname is set to 'anonymous'.

The following keyword arguments are commonly used: :keyword args: dictionary of request arguments :keyword authname: the name of the authenticated user, or 'anonymous' :keyword method: the HTTP request method :keyword path_info: the request path inside the application

Additionally cookie, format, language, lc_time, locale, remote_addr, remote_user, script_name, server_name, server_port and tz can be specified as keyword arguments.

Since: 1.0.11

locate(fn)

source code 

Locates a binary on the path.

Returns the fully-qualified path, or None.