tests.utils module#

Test utilities.

class tests.utils.AssertAPIErrorContextManager(code, info, msg, test_case, regex=None)[source]#

Bases: object

Context manager to assert certain APIError exceptions.

This is build similar to the unittest.TestCase.assertError implementation which creates a context manager. It then calls handle which either returns this manager if no executing object given or calls the callable object.

Create instance expecting the code and info.

handle(callable_obj, args, kwargs)[source]#

Handle the callable object by returning itself or using itself.

class tests.utils.DryDataSite(code, fam, user)[source]#

Bases: DrySite, DataSite

Dummy class to use instead of pywikibot.site.DataSite.

class tests.utils.DryPage(source, title='', ns=0)[source]#

Bases: Page

Dummy class that acts like a Page but avoids network activity.

Instantiate a Page object.

Parameters:

title (str) –

isDisambig()[source]#

Return disambig status stored in _disambig.

class tests.utils.DryParamInfo(*args, **kwargs)[source]#

Bases: dict

Dummy class to use instead of data.api.ParamInfo.

fetch(modules, _init=False)[source]#

Load dry data.

parameter(module, param_name)[source]#

Load dry data.

class tests.utils.DryRequest(*args, **kwargs)[source]#

Bases: CachedRequest

Dummy class to use instead of data.api.Request.

classmethod create_simple(req_site, **kwargs)[source]#

Skip CachedRequest implementation.

submit()[source]#

Prevented method.

class tests.utils.DrySite(code, fam, user)[source]#

Bases: APISite

Dummy class to use instead of pywikibot.site.APISite.

data_repository()[source]#

Return Site object for data repository e.g. Wikidata.

image_repository()[source]#

Return Site object for image repository e.g. commons.

linktrail()[source]#

Return default linkrail.

property userinfo#

Return dry data.

version()[source]#

Return a big dummy version string.

class tests.utils.DummySiteinfo(cache)[source]#

Bases: object

Dummy class to use instead of pywikibot.site.Siteinfo.

get(key, get_default=True, cache=True, expiry=False)[source]#

Return dry data.

get_requested_time(key)[source]#

Return False.

is_recognised(key)[source]#

Return None.

class tests.utils.FakeLoginManager(password=None, site=None, user=None)[source]#

Bases: LoginManager

Loads a fake password.

All parameters default to defaults in user-config.

Parameters:
  • site (Optional[pywikibot.site.BaseSite]) – Site object to log into

  • user (Optional[str]) – username to use. If user is None, the username is loaded from config.usernames.

  • password (Optional[str]) – password to use

Raises:

pywikibot.exceptions.NoUsernameError – No username is configured for the requested site.

property password#

Get the fake password.

class tests.utils.WarningSourceSkipContextManager(skip_list)[source]#

Bases: catch_warnings

Warning context manager that adjusts source of warning.

The source of the warning will be moved further down the stack to skip a list of objects that have been monkey patched into the call stack.

Parameters:

skip_list (list of object or (obj, str, int, int)) – List of objects to be skipped. The source of any warning that matches the skip_list won’t be adjusted.

property skip_list#

Return list of filename and line ranges to skip.

Return type:

list of (obj, str, int, int)

tests.utils.empty_sites()[source]#

Empty pywikibot _sites and _code_fam_from_url cache on entry point.

tests.utils.entered_loop(iterable)[source]#

Return True if iterable contains items.

tests.utils.execute(command, data_in=None, timeout=None, error=None)[source]#

Execute a command and capture outputs.

Parameters:

command (List[str]) – executable to run and arguments to use

tests.utils.execute_pwb(args, data_in=None, timeout=None, error=None, overrides=None)[source]#

Execute the pwb.py script and capture outputs.

Parameters:
  • args (Sequence[str]) – list of arguments for pwb.py

  • overrides (dict) – mapping of pywikibot symbols to test replacements

tests.utils.expected_failure_if(expect)[source]#

Unit test decorator to expect failure under conditions.

Parameters:

expect (bool) – Flag to check if failure is expected

tests.utils.fixed_generator(iterable)[source]#

Return a dummy generator ignoring all parameters.

This can be used to overwrite a generator method and yield predefined items:

>>> from tests.utils import fixed_generator
>>> site = pywikibot.Site()
>>> page = pywikibot.Page(site, 'Any page')
>>> list(page.linkedPages(total=1))
[]
>>> gen = fixed_generator([
...     pywikibot.Page(site, 'User:BobBot/Redir'),
...     pywikibot.Page(site, 'Main Page')])
>>> page.linkedPages = gen
>>> list(page.linkedPages(total=1))
[Page('Benutzer:BobBot/Redir'), Page('Main Page')]
tests.utils.skipping(*exceptions, msg=None)[source]#

Context manager to skip test on specified exceptions.

For example Eventstreams raises NotImplementedError if no streams parameter was given. Skip the following tests in that case:

with skipping(NotImplementedError):
    self.es = comms.eventstreams.EventStreams(streams=None)
self.assertIsInstance(self.es, tools.collections.GeneratorWrapper)

The exception message is used for the SkipTest reason. To use a custom message, add a msg parameter:

with skipping(AssertionError, msg='T304786'):
    self.assertEqual(self.get_mainpage().oldest_revision.text, text)

Multiple context expressions may also be used:

with (
    skipping(OtherPageSaveError),
    self.assertRaisesRegex(SpamblacklistError, 'badsite.com'),
):
    page.save()

Note

The last sample uses Python 3.10 syntax.

New in version 6.2.

Parameters:
  • msg (Optional[str]) – Optional skipping reason

  • exceptions (BaseException) – Exceptions to let test skip