Page MenuHomePhabricator

[recurring] Replace assertRaises with assertRaisesRegex
Open, LowPublic

Description

unittest.assertRaises is almost always lazy, and should be avoided.
The only desirable use of unittest.assertRaises is:

with self.assertRaises(ValueError) as cm:
    self.assertEqual(cm.exception, '.....')

unitest.assertRaisesRegex has an extra parameter to check the exception message, and should be used instead. By checking the exception message, the unit test verifies that the exception is precisely the one which was expected, rather than only of the same type as expected.
See: https://docs.python.org/3/library/unittest.html?highlight=assertraisesregex#unittest.TestCase.assertRaisesRegex

pywikibot hasunit test modules that use assertRaises and probably need upgrading:
https://codesearch.wmcloud.org/pywikibot/?q=assertRaises%5C(&i=nope&files=&excludeFiles=&repos=

Example: https://gerrit.wikimedia.org/r/#/c/330099/

Details

SubjectRepoBranchLines +/-
pywikibot/coremaster+9 -4
pywikibot/coremaster+6 -4
pywikibot/coremaster+42 -21
pywikibot/coremaster+184 -183
pywikibot/coremaster+28 -8
pywikibot/coremaster+73 -33
pywikibot/coremaster+53 -15
pywikibot/coremaster+21 -8
pywikibot/coremaster+3 -2
pywikibot/coremaster+38 -20
pywikibot/coremaster+11 -4
pywikibot/coremaster+36 -7
pywikibot/coremaster+99 -30
pywikibot/coremaster+9 -3
pywikibot/coremaster+39 -17
pywikibot/coremaster+9 -6
pywikibot/coremaster+22 -18
pywikibot/coremaster+45 -16
pywikibot/coremaster+7 -5
pywikibot/coremaster+7 -5
pywikibot/coremaster+2 -2
pywikibot/coremaster+50 -12
Show related patches Customize query in gerrit

Event Timeline

There are a very large number of changes, so older changes are hidden. Show Older Changes

Change 330099 had a related patch set uploaded (by MtDu):
Replace assertRaises with asserRaisesRegex in family_tests.py

https://gerrit.wikimedia.org/r/330099

Change 330099 merged by jenkins-bot:
Replace assertRaises with asserRaisesRegex in family_tests.py

https://gerrit.wikimedia.org/r/330099

Change 331141 had a related patch set uploaded (by SacredWKnight):
Replace assertRaises with asserRaisesRegex in tools_tests.py

https://gerrit.wikimedia.org/r/331141

Change 331160 had a related patch set uploaded (by Kenstin):
Replace assertRaises with assertRaisesRegex in bot_tests.py

https://gerrit.wikimedia.org/r/331160

Change 331224 had a related patch set uploaded (by Sn1per):
tests.page_tests: assertRaises => assertRaisesRegex

https://gerrit.wikimedia.org/r/331224

Change 331226 had a related patch set uploaded (by Preus):
Replace assertRaises with assertRaisesRegex in i18n_tests.py

https://gerrit.wikimedia.org/r/331226

Side note while working page_tests: tests.page_tests.TestPageDelete.test_delete doesn't seem to run properly for me:

tests: max_retries reduced from 25 to 1
Logging in to test:test as Sn1per@pywikibot
test_delete (tests.page_tests.TestPageDelete)
Test the site.delete and site.undelete method. ... Sleeping for 38.4 seconds, 2017-01-08 17:10:45
Page [[User:Unicodesnowman/DeleteTest]] saved
 39.129s FAIL

======================================================================
FAIL: test_delete (tests.page_tests.TestPageDelete)
Test the site.delete and site.undelete method.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/gmon/devel/pywikibot-core/tests/page_tests.py", line 897, in test_delete
    self.assertEqual(p.isRedirectPage(), True)
AssertionError: False != True

----------------------------------------------------------------------
Ran 1 test in 41.386s

FAILED (failures=1)

I think the page edit fails because the edits involved in the test do not show up on the testwiki end.

Change 331226 abandoned by Preus:
Replace assertRaises with assertRaisesRegex in i18n_tests.py

https://gerrit.wikimedia.org/r/331226

Change 331396 had a related patch set uploaded (by Preus):
Replace assertRaises with assertRaisesRegex in ui_options_tests.py

https://gerrit.wikimedia.org/r/331396

Change 331396 merged by jenkins-bot:
Replace assertRaises with assertRaisesRegex in ui_options_tests.py

https://gerrit.wikimedia.org/r/331396

Change 331160 merged by jenkins-bot:
Replace assertRaises with assertRaisesRegex in bot_tests.py

https://gerrit.wikimedia.org/r/331160

Change 331601 had a related patch set uploaded (by Divadsn):
Replace assertRaises with assertRaisesRegex in http_tests.py

https://gerrit.wikimedia.org/r/331601

Change 331141 merged by jenkins-bot:
Replace assertRaises with asserRaisesRegex in tools_tests.py

https://gerrit.wikimedia.org/r/331141

Change 331601 merged by jenkins-bot:
Replace assertRaises with assertRaisesRegex in http_tests.py

https://gerrit.wikimedia.org/r/331601

Change 331913 had a related patch set uploaded (by Awu42):
Replace assertRaises with assertRaisesRegex in mediawikiversion_tests.py

https://gerrit.wikimedia.org/r/331913

Change 331913 merged by jenkins-bot:
Replace assertRaises with assertRaisesRegex in mediawikiversion_tests.py

https://gerrit.wikimedia.org/r/331913

Change 331224 merged by jenkins-bot:
tests.page_tests: assertRaises => assertRaisesRegex

https://gerrit.wikimedia.org/r/331224

Change 342325 had a related patch set uploaded (by Dalba):
[pywikibot/core] interwikidata_tests.py: Do not change config while performing tests

https://gerrit.wikimedia.org/r/342325

Change 342325 merged by jenkins-bot:
[pywikibot/core] interwikidata_tests.py: Do not change config while performing tests

https://gerrit.wikimedia.org/r/342325

Dalba triaged this task as Low priority.May 5 2017, 3:34 AM
Xqt claimed this task.

This is not complete, and is suitable for GCI 2017

jayvdb updated the task description. (Show Details)
jayvdb added a subscriber: Xqt.

@jayvdb I could help mentor this. Is the list above updated?

jayvdb renamed this task from Replace assertRaises with assertRaisesRegex to [recurring] Replace assertRaises with assertRaisesRegex.Oct 28 2017, 3:18 AM

Change 393990 had a related patch set uploaded (by Rafidaslam; owner: rafid):
[pywikibot/core@master] Change assertRaises to assertRaisesRegex in tests/file_tests.py

https://gerrit.wikimedia.org/r/393990

Change 394520 had a related patch set uploaded (by Rafidaslam; owner: rafid):
[pywikibot/core@master] Change to use assertRaisesRegex in link_tests.py

https://gerrit.wikimedia.org/r/394520

Change 394520 merged by jenkins-bot:
[pywikibot/core@master] Change to use assertRaisesRegex in link_tests.py

https://gerrit.wikimedia.org/r/394520

Change 393990 merged by jenkins-bot:
[pywikibot/core@master] Change to use assertRaisesRegex in file_tests.py

https://gerrit.wikimedia.org/r/393990

Change 396576 had a related patch set uploaded (by Divadsn; owner: Divadsn):
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in category_tests.py

https://gerrit.wikimedia.org/r/396576

Change 396576 merged by jenkins-bot:
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in category_tests.py

https://gerrit.wikimedia.org/r/396576

Change 396589 had a related patch set uploaded (by Divadsn; owner: Divadsn):
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in namespace_tests.py

https://gerrit.wikimedia.org/r/396589

Change 396589 merged by jenkins-bot:
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in namespace_tests.py

https://gerrit.wikimedia.org/r/396589

Change 399134 had a related patch set uploaded (by Ryan10145; owner: Ryan10145):
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in isbn_tests.py

https://gerrit.wikimedia.org/r/399134

Change 399202 had a related patch set uploaded (by Clockery; owner: Clockery):
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in login_tests.py

https://gerrit.wikimedia.org/r/399202

Change 399202 merged by jenkins-bot:
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in login_tests.py

https://gerrit.wikimedia.org/r/399202

Change 399134 merged by jenkins-bot:
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in isbn_tests.py

https://gerrit.wikimedia.org/r/399134

Change 399348 had a related patch set uploaded (by Xqt; owner: Ryan10145):
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in isbn_tests.py

https://gerrit.wikimedia.org/r/399348

Change 399399 had a related patch set uploaded (by Clockery; owner: cf):
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in flow_tests.py

https://gerrit.wikimedia.org/r/399399

Change 404085 had a related patch set uploaded (by Divadsn; owner: Divadsn):
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in isbn_tests.py

https://gerrit.wikimedia.org/r/404085

divadsn updated the task description. (Show Details)

Change 404085 merged by jenkins-bot:
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in isbn_tests.py

https://gerrit.wikimedia.org/r/404085

Change 404285 had a related patch set uploaded (by Clockery; owner: cf):
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in api_tests.py

https://gerrit.wikimedia.org/r/404285

Change 399348 abandoned by Dalba:
Replace assertRaises with assertRaisesRegex in isbn_tests.py

Reason:
9a13055ce9c130d832ebd273681e7bf4d5dd71a7

https://gerrit.wikimedia.org/r/399348

Change 399348 restored by Dalba:
Replace assertRaises with assertRaisesRegex in isbn_tests.py

Reason:
T185317

https://gerrit.wikimedia.org/r/399348

Any updates on this? If there is any work pending here we can move this to Google-Code-in-2019?

Any updates on this? If there is any work pending here we can move this to Google-Code-in-2019?

Sure.

Xqt removed jayvdb as the assignee of this task.Mar 26 2020, 7:20 PM

Change 399348 merged by Xqt:
[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in isbn_tests.py

https://gerrit.wikimedia.org/r/399348

Change 684429 had a related patch set uploaded (by Ngwebecky96; author: Ngwebecky96):

[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in the several unittest files

https://gerrit.wikimedia.org/r/684429

Xqt updated the task description. (Show Details)

Change 684429 abandoned by Xqt:

[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in the several unittest files

Reason:

Not appropriate

https://gerrit.wikimedia.org/r/684429

Change 404285 abandoned by Xqt:

[pywikibot/core@master] Replace assertRaises with assertRaisesRegex in api_tests.py

Reason:

too old, some tests were removed

https://gerrit.wikimedia.org/r/404285

Change 810382 had a related patch set uploaded (by Vivian Rook; author: Vivian Rook):

[pywikibot/core@master] [IMPR] move to assertRaisesRegex

https://gerrit.wikimedia.org/r/810382

Change 810382 merged by jenkins-bot:

[pywikibot/core@master] [IMPR] move to assertRaisesRegex

https://gerrit.wikimedia.org/r/810382

I see one more of these in tests/utils.py though I'm unclear about how this is run. It does not appear to run through pytest (I would update tox.ini with a specific file to test, this one doesn't match the *tests.py pattern and ignoring that doesn't get it to run regardless). How would one run just the tests in utils.py?

I see one more of these in tests/utils.py though I'm unclear about how this is run. It does not appear to run through pytest (I would update tox.ini with a specific file to test, this one doesn't match the *tests.py pattern and ignoring that doesn't get it to run regardless). How would one run just the tests in utils.py?

utils.py is not a test file itself but a library module for tests. All test files are ending with "tests" and are collected in tests.__init__.py, utils.py uses assertRaises inside the contextmanager AssertAPIErrorContextManager. The contextmanager is used in aspects to implement the assertAPIError method which is used for tests found here. For example you can test the flow_thanks_tests as follows:

  1. login your bot on testwiki: pwb login -site:wikipedia:test
  2. set the environment variable PYWIKIBOT_TEST_WRITE to 1 to enable write tests
  3. run the test script loacally: pwb -site:wikipedia:test flow_thanks_tests -v TestThankFlowPost.test_self_thank

The AssertAPIErrorContextManager contextmanager needs the exception message regex somehow e.g.:

def __init__(self, code, info, msg, test_case, regex=None):
    """Create instance expecting the code and info."""
    self.code = code
    self.info = info
    self.msg = msg
    self.test_case = test_case
    self.regex = regex

def __enter__(self):
    """Enter this context manager and the unittest's context manager."""
    if self.regex:
        self.cm = self.test_case.assertRaisesRegex(APIError, self.regex, msg=self.msg)
    else:
        self.cm = self.test_case.assertRaises(APIError, msg=self.msg)
    self.cm.__enter__()
    return self.cm

Change 814235 had a related patch set uploaded (by Vivian Rook; author: Vivian Rook):

[pywikibot/core@master] [IMPR] Replace assertRaises with assertRaisesRegex

https://gerrit.wikimedia.org/r/814235

Change 814235 merged by jenkins-bot:

[pywikibot/core@master] [IMPR] Replace assertRaises with assertRaisesRegex

https://gerrit.wikimedia.org/r/814235