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

Related Changes in Gerrit:
SubjectRepoBranchLines +/-
pywikibot/coremaster+102 -52
pywikibot/coremaster+73 -33
pywikibot/coremaster+9 -4
pywikibot/coremaster+6 -4
pywikibot/coremaster+42 -21
pywikibot/coremaster+184 -183
pywikibot/coremaster+28 -8
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 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

Change #399399 abandoned by Xqt:

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

Reason:

too old anf flow support will be dropped

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

Hello! @jayvdb can i be assigned this? Im new to more organized large scale open-source projects and would like to begin my contribution to them with wikimedia.

Hello! @jayvdb can i still work on this? are there more assertions to be changed/updated to regex?

Hi @Chickenleaf, you may work on this issue if you want to.
Here are the remaining parts to improve: https://codesearch.wmcloud.org/pywikibot/?q=assertRaises%5C(&i=nope&files=&excludeFiles=&repos=
but ignore tests/flow_tests.py because that module will be removed.

Change #1187987 had a related patch set uploaded (by Tejashxv; author: Tejashxv):

[pywikibot/core@master] upgrade assertRaises to assertRaisesRegex for better error validation

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

Change #1187987 merged by jenkins-bot:

[pywikibot/core@master] Upgrade assertRaises to assertRaisesRegex

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

Hi @Xqt, I noticed the previous patch was merged, but I ran the code search and found about 30 files that still use 'assertRaises' (like api_tests.py and datasite_tests.py). May I work on fixing these remaining instances?

Xqt removed Tejashxv as the assignee of this task.Tue, Feb 3, 3:04 PM
Xqt added a subscriber: Tejashxv.

Hi @Bharathvk1889, sure, there are still some remaining issues that you can work on. Please don’t try to fix them all in a single patch — split them, for example, by test file, as it’s easier to review smaller parts. I also recommend installing pre-commit and running the tool before submitting the patch. Please note that the repository is on Gerrit, not GitHub.

Hi! I'm a beginner and would like to work on this task. Can someone guide me on how to start? I have set up my Gerrit account and SSH keys.

Hi @Bharathvk1889 and @VishakhaSahu05, you can both work on this task. However, please make sure to coordinate and announce in advance which test files you intend to work on. Please also see the description in the introduction.

Important links