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.


title (str)


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.


Prevented method.

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

Bases: APISite

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


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


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


Return default linkrail.

login(*args, cookie_only=False, **kwargs)[source]#

Overwrite login which is called when a site is initialized.

Added in version 8.0.4.

property userinfo#

Return dry data.


Return a big dummy version string.

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

Bases: object

Dummy Siteinfo class.

To be used instead of pywikibot.site.Siteinfo.

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

Return dry data.


Return False.


Return whether the key is cached.

Added in version 8.3.


key (str)

Return type:



Return None.

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

Bases: ClientLoginManager

Loads a fake password.

All parameters default to defaults in user-config.

  • site (pywikibot.site.BaseSite | None) – Site object to log into

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

  • password (str | None) – password to use


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.


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)


Empty pywikibot _sites and _code_fam_from_url cache on entry point.


Return True if iterable contains items.

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

Execute a command and capture outputs.

Changed in version 8.2: error parameter was removed.

Changed in version 9.1: parameters except command are keyword only.


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

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

Execute the pwb.py script and capture outputs.

Changed in version 8.2: the error parameter was removed.

Changed in version 9.1: parameters except args are keyword only.

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

  • overrides (dict[str, str] | None) – mapping of pywikibot symbols to test replacements

  • data_in (Sequence[str] | None)

  • timeout (int | float | None)

Return type:

dict[str, Any]


Unit test decorator to expect failure under conditions.


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


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 (
    self.assertRaisesRegex(SpamblacklistError, 'badsite.com'),


The last sample uses Python 3.10 syntax.

Added in version 6.2.

  • msg (str | None) – Optional skipping reason

  • exceptions (BaseException) – Exceptions to let test skip