l.t.__init__ : module documentation

Part of lp.testing

No module docstring
Function admin_logged_in Undocumented
Function anonymous_logged_in Make a context manager for running with the anonymous log in.
Function celebrity_logged_in Make a context manager for running logged in as a celebrity.
Function login Simulates a login, using the specified email.
Function login_admin Log in as an admin.
Function login_as Login as a person or a team.
Function login_celebrity Login as a celebrity.
Function login_person Login the person with their preferred email.
Function login_team Login as a member of 'team'.
Function logout Tear down after login(...), ending the current interaction.
Function person_logged_in Make a context manager for running logged in as 'person'.
Function run_with_login Run 'function' with 'person' logged in.
Function with_celebrity_logged_in Decorate a function so that it's run with a celebrity logged in.
Function with_person_logged_in Undocumented
Function api_url Find the web service URL of a data model object.
Function launchpadlib_credentials_for Create launchpadlib credentials for the given person.
Function launchpadlib_for Create a Launchpad object for the given person.
Function oauth_access_token_for Find or create an OAuth access token for the given person.
Function reset_logging Reset the logging system back to defaults
Class FakeTime Provides a controllable implementation of time.time().
Class StormStatementRecorder A Storm tracer to record all database queries.
Class RequestTimelineCollector Collect timeline events logged in web requests.
Function record_statements Run the function and record the sql statements that are executed.
Function record_two_runs A helper that returns the two storm statement recorders
Function run_with_storm_debug A helper function to run a function with storm debug tracing on.
Class TestCase Provide Launchpad-specific test facilities.
Class TestCaseWithFactory No class docstring; 5/8 methods, 1/1 static methods documented
Class BrowserTestCase A TestCase class for browser tests.
Class WebServiceTestCase Test case optimized for testing the web service using launchpadlib.
Class AbstractYUITestCase No class docstring; 3/4 methods documented
Class YUIUnitTestCase Undocumented
Function build_yui_unittest_suite Undocumented
Class ZopeTestInSubProcess Run tests in a sub-process, respecting Zope idiosyncrasies.
Class EventRecorder Intercept and record Zope events.
Function feature_flags Provide a context in which feature flags work.
Function time_counter A generator for yielding datetime values.
Function run_script Run the given command line as a subprocess.
Function run_process Run the given command as a subprocess.
Function normalize_whitespace Replace all sequences of whitespace with a single space.
Function map_branch_contents Return all files in branch at branch_url.
Function set_feature_flag Set a feature flag to the specified value.
Function validate_mock_class Validate method signatures in mock classes derived from real classes.
Function ws_object Convert an object into its webservice version.
Function monkey_patch In the ContextManager scope, monkey-patch values.
Function unlink_source_packages Remove all links between the product and source packages.
Class ExpectedException An ExpectedException that provides access to the caught exception.
Function extract_lp_cache Undocumented
Function nonblocking_readline Non-blocking readline.
Class FakeLaunchpadRequest No class docstring; 1/1 methods documented
Class FakeAdapterMixin A testcase mixin that helps register/unregister Zope adapters.
Function clean_up_reactor Undocumented
Function verifyObject A specialized verifyObject which removes the security proxy of the
Function _harvest_yui_test_files Undocumented
@contextmanager
def admin_logged_in():
Undocumented
@contextmanager
def anonymous_logged_in():
Make a context manager for running with the anonymous log in.
@contextmanager
def celebrity_logged_in(celebrity_name):
Make a context manager for running logged in as a celebrity.
def login(email, participation=None):
Simulates a login, using the specified email.

If the lp.testing.ANONYMOUS constant is supplied as the email, you'll be logged in as the anonymous user.

You can optionally pass in a participation to be used. If no participation is given, a LaunchpadTestRequest is used.

If the participation provides IPublicationRequest, it must implement setPrincipal(), otherwise it must allow setting its principal attribute.

def login_admin(participation=None):
Log in as an admin.
def login_as(person_or_team, participation=None):
Login as a person or a team.
Parametersperson_or_teamA person, a team, ANONYMOUS or None. None and ANONYMOUS are equivalent, and will log the person in as the anonymous user.
def login_celebrity(celebrity_name, participation=None):
Login as a celebrity.
def login_person(person, participation=None):
Login the person with their preferred email.
def login_team(team, participation=None):
Login as a member of 'team'.
def logout():
Tear down after login(...), ending the current interaction.

Note that this is done automatically in LaunchpadFunctionalTestCase's tearDown method so you generally won't need to call this.

@contextmanager
def person_logged_in(person):
Make a context manager for running logged in as 'person'.
ParameterspersonA person, an account, a team or ANONYMOUS. If a team, will log in as an arbitrary member of that team.
def run_with_login(person, function, *args, **kwargs):
Run 'function' with 'person' logged in.
def with_celebrity_logged_in(celebrity_name):
Decorate a function so that it's run with a celebrity logged in.
def with_person_logged_in(person):
Undocumented
def api_url(obj):
Find the web service URL of a data model object.

This makes it easy to load up the factory object you just created in launchpadlib.

ParametersWhich web service version to use.
ReturnsA relative URL suitable for passing into Launchpad.load().
def launchpadlib_credentials_for(consumer_name, person, permission=OAuthPermission.WRITE_PRIVATE, context=None):
Create launchpadlib credentials for the given person.
Parametersconsumer_nameAn OAuth consumer name.
personA person (or the name of a person) for whom to create or find credentials.
permissionAn OAuthPermission (or its token) designating the level of permission the credentials should have.
contextThe OAuth context for the credentials.
ReturnsA launchpadlib Credentials object.
def launchpadlib_for(consumer_name, person=None, permission=OAuthPermission.WRITE_PRIVATE, context=None, version='devel', service_root='http://api.launchpad.test/'):
Create a Launchpad object for the given person.
Parametersconsumer_nameAn OAuth consumer name.
personA person (or the name of a person) for whom to create or find credentials.
permissionAn OAuthPermission (or its token) designating the level of permission the credentials should have.
contextThe OAuth context for the credentials.
versionThe version of the web service to access.
service_rootThe root URL of the web service to access.
ReturnsA launchpadlib Launchpad object.
def oauth_access_token_for(consumer_name, person, permission, context=None):
Find or create an OAuth access token for the given person.
:param consumer_name: An OAuth consumer name.
:param person: A person (or the name of a person) for whom to create
    or find credentials.
:param permission: An OAuthPermission (or its token) designating
    the level of permission the credentials should have.
:param context: The OAuth context for the credentials (or a string
    designating same).

:return: A tuple of an OAuthAccessToken object and its secret.
def reset_logging():
Reset the logging system back to defaults

Currently, defaults means 'the way the Z3 testrunner sets it up' plus customizations made in lp_sitecustomize

def record_statements(function, *args, **kwargs):
Run the function and record the sql statements that are executed.
Returnsa tuple containing the return value of the function, and a list of sql statements.
def record_two_runs(tested_method, item_creator, first_round_number, second_round_number=None, login_method=None, record_request=False):
A helper that returns the two storm statement recorders obtained when running tested_method after having run the method {item_creator} {first_round_number} times and then again after having run the same method {second_round_number} times.

If {login_method} is not None, it is called before each batch of {item_creator} calls.

If {record_request} is True, RequestTimelineCollector is used to get the query counts, so {tested_method} should make a web request. Otherwise, StormStatementRecorder is used to get the query count.

Returnsa tuple containing the two recorders obtained by the successive runs.
def run_with_storm_debug(function, *args, **kwargs):
A helper function to run a function with storm debug tracing on.
def build_yui_unittest_suite(app_testing_path, yui_test_class):
Undocumented
def _harvest_yui_test_files(file_path):
Undocumented
@contextmanager
def feature_flags():
Provide a context in which feature flags work.
def time_counter(origin=None, delta=timedelta(5)):

A generator for yielding datetime values.

Each time the generator yields a value, the origin is incremented by the delta.

>>> now = time_counter(datetime(2007, 12, 1), timedelta(days=1))
>>> now.next()
datetime.datetime(2007, 12, 1, 0, 0)
>>> now.next()
datetime.datetime(2007, 12, 2, 0, 0)
>>> now.next()
datetime.datetime(2007, 12, 3, 0, 0)
def run_script(cmd_line, env=None, cwd=None):
Run the given command line as a subprocess.
Parameterscmd_lineA command line suitable for passing to subprocess.Popen.
envAn optional environment dict. If none is given, the script will get a copy of your present environment. Either way, PYTHONPATH will be removed from it because it will break the script.
ReturnsA 3-tuple of stdout, stderr, and the process' return code.
def run_process(cmd, env=None):
Run the given command as a subprocess.

This differs from run_script in that it does not execute via a shell and it explicitly connects stdin to /dev/null so that processes will not be able to hang, waiting for user input.

Parameterscmd_lineA command suitable for passing to subprocess.Popen.
envAn optional environment dict. If none is given, the script will get a copy of your present environment. Either way, PYTHONPATH will be removed from it because it will break the script.
ReturnsA 3-tuple of stdout, stderr, and the process' return code.
def normalize_whitespace(string):
Replace all sequences of whitespace with a single space.
def map_branch_contents(branch):
Return all files in branch at branch_url.
Parametersbranch_urlthe URL for an accessible branch.
Returnsa dict mapping file paths to file contents. Only regular files are included.
def set_feature_flag(name, value, scope=u'default', priority=1):
Set a feature flag to the specified value.

In order to access the flag, use the feature_flags context manager or set the feature controller in some other way. :param name: The name of the flag. :param value: The value of the flag. :param scope: The scope in which the specified value applies.

def validate_mock_class(mock_class):

Validate method signatures in mock classes derived from real classes.

We often use mock classes in tests which are derived from real classes.

This function ensures that methods redefined in the mock class have the same signature as the corresponding methods of the base class.

>>> class A:
...
...     def method_one(self, a):
...         pass
>>>
>>> class B(A):
...     def method_one(self, a):
...        pass
>>> validate_mock_class(B)

If a class derived from A defines method_one with a different signature, we get an AssertionError.

>>> class C(A):
...     def method_one(self, a, b):
...        pass
>>> validate_mock_class(C)
Traceback (most recent call last):
...
AssertionError: Different method signature for method_one:...

Even a parameter name must not be modified.

>>> class D(A):
...     def method_one(self, b):
...        pass
>>> validate_mock_class(D)
Traceback (most recent call last):
...
AssertionError: Different method signature for method_one:...

If validate_mock_class() for anything but a class, we get an AssertionError.

>>> validate_mock_class('a string')
Traceback (most recent call last):
...
AssertionError: validate_mock_class() must be called for a class
def ws_object(launchpad, obj):
Convert an object into its webservice version.
ParameterslaunchpadThe Launchpad instance to convert from.
objThe object to convert.
ReturnsA launchpadlib Entry object.
@contextmanager
def monkey_patch(context, **kwargs):
In the ContextManager scope, monkey-patch values.

The context may be anything that supports setattr. Packages, modules, objects, etc. The kwargs are the name/value pairs for the values to set.

def unlink_source_packages(product):
Remove all links between the product and source packages.

A product cannot be deactivated if it is linked to source packages.

def extract_lp_cache(text):
Undocumented
def nonblocking_readline(instream, timeout):
Non-blocking readline.

Files must provide a valid fileno() method. This is a test helper as it is inefficient and unlikely useful for production code.

def clean_up_reactor():
Undocumented
def verifyObject(iface, candidate, tentative=0):
A specialized verifyObject which removes the security proxy of the object before verifying it.
API Documentation for Launchpad, generated by pydoctor at 2022-06-16 00:00:12.