tests.aspects module#
Test aspects to allow fine grained control over what tests are executed.
Several parts of the test infrastructure are implemented as mixins, such as API result caching and excessive test durations.
- class tests.aspects.AlteredDefaultSiteTestCase(*args, **kwargs)[source]#
Bases:
TestCaseSave and restore the config.mylang and config.family.
- abstract_class = True#
- class tests.aspects.CheckHostnameMixin(methodName='runTest')[source]#
Bases:
TestCaseBaseCheck the hostname is online before running tests.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
- class tests.aspects.DebugOnlyTestCase(*args, **kwargs)[source]#
Bases:
TestCaseTest cases that only operate in debug mode.
- abstract_class = True#
- class tests.aspects.DefaultDrySiteTestCase(*args, **kwargs)[source]#
Bases:
DefaultSiteTestCaseRun tests using the config specified site in offline mode.
- abstract_class = True#
- dry = True#
- class tests.aspects.DefaultSiteTestCase(*args, **kwargs)[source]#
Bases:
TestCaseRun tests against the config specified site.
- abstract_class = True#
- code = 'test'#
- family = 'wikipedia'#
- class tests.aspects.DefaultWikibaseClientTestCase(*args, **kwargs)[source]#
Bases:
WikibaseClientTestCase,DefaultSiteTestCaseRun tests against any site connected to a Wikibase.
- abstract_class = True#
- class tests.aspects.DefaultWikidataClientTestCase(*args, **kwargs)[source]#
Bases:
DefaultWikibaseClientTestCaseRun tests against any site connected to Wikidata.
- abstract_class = True#
- class tests.aspects.DeprecationTestCase(*args, **kwargs)[source]#
Bases:
DebugOnlyTestCase,TestCaseTest cases for deprecation function in the tools module.
- INSTEAD = <object object>#
- NO_INSTEAD = <object object>#
- abstract_class = True#
- assertDeprecation(msg=None)[source]#
Assert that a deprecation warning happened.
- Parameters:
msg (str or None or INSTEAD or NO_INSTEAD) – Either the specific message or None to allow any generic message. When set to
INSTEADit only counts those supplying an alternative and whenNO_INSTEADonly those not supplying one.
- assertDeprecationParts(deprecated=None, instead=None)[source]#
Assert that a deprecation warning happened.
To simplify deprecation tests it just requires the to separated parts and forwards the result to
assertDeprecation.- Parameters:
deprecated (str or None) – The deprecated string. If None it uses a generic match depending on instead.
instead (str or None or True or False) – The instead string unless deprecated is None. If it’s None it allows any generic deprecation string, on True only those where instead string is present and on False only those where it’s missing. If the deprecation string is not None, no instead string is expected when instead evaluates to False.
- assertOneDeprecation(msg=None, count=1)[source]#
Assert that exactly one deprecation message happened and reset.
- assertOneDeprecationParts(deprecated=None, instead=None, count=1)[source]#
Assert that exactly one deprecation message happened and reset.
It uses the same arguments as
assertDeprecationParts.
- property deprecation_messages#
Return captured deprecation warnings.
- source_adjustment_skips = [<class 'unittest.case._AssertRaisesContext'>, <function TestCase.assertRaises>, <function TestCase.assertRaisesRegex>, <class 'unittest.case._AssertRaisesBaseContext'>]#
- class tests.aspects.DisableSiteMixin(methodName='runTest')[source]#
Bases:
TestCaseBaseTest cases not connected to a Site object.
Do not use this for mock Site objects.
Never set a class or instance variable called ‘site’ As it will prevent tests from executing when invoked as: $ pytest -a ‘not site’
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
- class tests.aspects.DisconnectedSiteMixin(methodName='runTest')[source]#
Bases:
TestCaseBaseTest cases using a disconnected Site object.
Do not use this for mock Site objects.
Never set a class or instance variable called ‘site’ As it will prevent tests from executing when invoked as: $ pytest -a ‘not site’
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
- class tests.aspects.ForceCacheMixin(methodName='runTest')[source]#
Bases:
TestCaseBaseAggressively cached API test cases.
Patches pywikibot.data.api to aggressively cache API responses.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
- class tests.aspects.HttpbinTestCase(*args, **kwargs)[source]#
Bases:
TestCaseCustom test case class, which allows dry httpbin tests with pytest-httpbin.
Test cases, which use httpbin, need to inherit this class.
- abstract_class = True#
- get_httpbin_hostname()[source]#
Return httpbin hostname.
If pytest is used, returns hostname of local httpbin server. Otherwise, returns: httpbin.org
- get_httpbin_url(path='')[source]#
Return url of httpbin.
If pytest is used, returns url of local httpbin server. Otherwise, returns: http://httpbin.org
- sites = {'httpbin': {'hostname': 'httpbin.org'}}#
- class tests.aspects.MetaTestCaseClass(name, bases, dct)[source]#
Bases:
typeTest meta class.
Create the new class.
- class tests.aspects.NeedRightsMixin(methodName='runTest')[source]#
Bases:
TestCaseBaseRequire specific rights.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
- class tests.aspects.PatchingTestCase(*args, **kwargs)[source]#
Bases:
TestCaseEasily patch and unpatch instances.
- abstract_class = True#
- class tests.aspects.PwbTestCase(*args, **kwargs)[source]#
Bases:
TestCaseTest cases use pwb.py to invoke scripts.
Test cases which use pwb typically also access a site, and use the network. Even during initialisation, scripts may call pywikibot.handle_args, which initialises loggers and uses the network to determine if the code is stale.
The flag ‘pwb’ is used by the TestCase metaclass to check that a test site is set declared in the class properties, or that ‘site = False’ is added to the class properties in the unlikely scenario that the test case uses pwb in a way that doesn’t use a site.
If a test class is marked as ‘site = False’, the metaclass will also check that the ‘net’ flag is explicitly set.
- abstract_class = True#
- pwb = True#
- class tests.aspects.RecentChangesTestCase(*args, **kwargs)[source]#
Bases:
WikimediaDefaultSiteTestCaseTest cases for tests that use recent change.
- abstract_class = True#
- length = 3#
- class tests.aspects.RequireLoginMixin(methodName='runTest')[source]#
Bases:
TestCaseBaseRun tests against a specific site, with a login.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
- login = True#
- classmethod require_site_user(family, code)[source]#
Check the user config has a valid login to the site.
- class tests.aspects.ScriptMainTestCase(*args, **kwargs)[source]#
Bases:
AlteredDefaultSiteTestCaseTests that depend on the default site being set to the test site.
- abstract_class = True#
- class tests.aspects.SiteAttributeTestCase(*args, **kwargs)[source]#
Bases:
TestCaseAdd the sites as attributes to the instances.
- abstract_class = True#
- class tests.aspects.SiteNotPermitted(code, fam=None, user=None)[source]#
Bases:
BaseSiteSite interface to prevent sites being loaded.
- class tests.aspects.SiteWriteMixin(methodName='runTest')[source]#
Bases:
TestCaseBaseTest cases involving writing to the server.
When editing, the API should not be patched to use CachedRequest. This class prevents that.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
- classmethod setUpClass()[source]#
Set up the test class.
Reject write test classes configured with non-test wikis, or caching.
Prevent test classes from writing to the site by default.
If class attribute ‘write’ is -1, the test class is skipped unless environment variable PYWIKIBOT_TEST_WRITE_FAIL is set to 1.
Otherwise the test class is skipped unless environment variable PYWIKIBOT_TEST_WRITE is set to 1.
- class tests.aspects.TestCase(*args, **kwargs)[source]#
Bases:
TestCaseBaseRun tests on pre-defined sites.
- abstract_class = True#
- class tests.aspects.TestCaseBase(methodName='runTest')[source]#
Bases:
TestTimerMixinBase class for all tests.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
- assertAPIError(code, info=None, callable_obj=None, *args, regex=None, **kwargs)[source]#
Assert that a specific APIError wrapped around
assertRaises.If no callable object is defined and it returns a context manager, that context manager will return the underlying context manager used by
assertRaises. So it’s possible to access the APIError by using it’sexceptionattribute.- Parameters:
code (str) – The code of the error which must have happened.
info (str or None) – The info string of the error or None if no it shouldn’t be checked.
callable_obj (callable) – The object that will be tested. If None it returns a context manager like
assertRaises.args – The positional arguments forwarded to the callable object.
kwargs – The keyword arguments forwarded to the callable object.
- Returns:
Context manager if callable_obj is None and None otherwise.
- Return type:
None or context manager
- assertPageInNamespaces(page, namespaces)[source]#
Assert that Pages is in namespaces.
- Parameters:
page (pywikibot.BasePage) – Page
namespaces (int or set of int) – expected namespaces
- assertPageTitlesCountEqual(gen, titles, site=None)[source]#
Test that pages in gen match expected titles, regardless of order.
Only iterates to the length of titles plus two.
- Parameters:
gen (Iterable[Page]) – Page generator
titles (iterator) – Expected titles
site (pywikibot.site.APISite) – Site of expected pages
- assertPageTitlesEqual(gen, titles, site=None)[source]#
Test that pages in gen match expected titles.
Only iterates to the length of titles plus two.
- Parameters:
gen (Iterable[Page]) – Page generator
titles (iterator) – Expected titles
site (pywikibot.site.APISite) – Site of expected pages
- class tests.aspects.TestTimerMixin(methodName='runTest')[source]#
Bases:
TestCaseTime each test and report excessive durations.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
- test_duration_warning_interval = 10#
- class tests.aspects.WikibaseClientTestCase(*args, **kwargs)[source]#
Bases:
WikibaseTestCaseRun tests against a specific site connected to a wikibase.
- abstract_class = True#
- class tests.aspects.WikibaseTestCase(*args, **kwargs)[source]#
Bases:
TestCaseRun tests against a wikibase site.
- abstract_class = True#
- classmethod setUpClass()[source]#
Set up the test class.
Checks that all sites are configured with a Wikibase repository, with Site.has_data_repository() returning True, and all sites use the same data repository.
- wikibase = True#
- class tests.aspects.WikidataTestCase(*args, **kwargs)[source]#
Bases:
WikibaseTestCaseTest cases use Wikidata.
- abstract_class = True#
- cached = True#
- code = 'wikidata'#
- family = 'wikidata'#
- class tests.aspects.WikimediaDefaultSiteTestCase(*args, **kwargs)[source]#
Bases:
DefaultSiteTestCaseTest class to run against a WMF site, preferring the default site.
- abstract_class = True#
- tests.aspects.optional_pytest_httpbin_cls_decorator(f)[source]#
Empty decorator in case pytest_httpbin is not installed.
- tests.aspects.require_modules(*required_modules)[source]#
Require that the given list of modules can be imported.
- tests.aspects.require_version(version_needed, reason='')[source]#
Require minimum MediaWiki version to be queried.
The version needed for the test; must be given with a preleading rich comparisons operator like
<1.27wmf4or>=1.39. If the comparison does not match the test will be skipped.This decorator can only be used for TestCase having a single site. It cannot be used for DrySite tests. In addition version comparison for other than the current site e.g. for the related data or image repositoy of the current site is ot possible.
New in version 8.0.0.
- Parameters:
version_needed (str) – The version needed
reason (str) – A reason for skipping the test.
- Raises:
Exception – Usage validation fails