This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Unittest - commenting passing tests cause previous failing tests to pass
Type: behavior Stage: resolved
Components: Tests Versions: Python 3.9
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: nuse, steven.daprano
Priority: normal Keywords:

Created on 2021-11-17 04:28 by nuse, last changed 2022-04-11 14:59 by admin. This issue is now closed.

File name Uploaded Description Edit nuse, 2021-11-17 04:28
Messages (2)
msg406449 - (view) Author: Jamie Chaisson (nuse) * Date: 2021-11-17 04:28
Ubuntu Release 20.04.3 LTS (Focal Fossa) 64-bit
Using unittest, testing with assertEqual on int values and known good output. Unittest produces one-off error on handful of edge-case tests causing assert to fail. Commenting out passing assertEquals tests to isolate failing tests causes previous failing tests to pass (reproducible). All input values produce output values when testing by hand, only different when using unittest.

Pardon the homework. Removing the class and moving member variables into my_datetime function resolves the issue; however, the behavior as written should not be happening.
msg406453 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-11-17 04:46
At a quick glance, I am 95% sure the problem lies in these two snippets of your code, not unittest:

    class My_Time:
        months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

    if is_leapyr(year):
        my_time.months[1] = 29

The first snippet makes months, a mutable list, a class attribute, which means it is shared by all instances of the class. The second snippet mutates that list, which means every single instance will see the same value.

So commenting out some tests will change whether or not the shared list gets mutated, which will change whether or not other tests pass or fail.

I think the smallest change you need make to fix your code is to put the initialisation of My_Time into an `__init__` method, so that the attributes (including the list) are no longer shared between all instances.

    class My_Time:
        def __init__(self):
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 89985
2021-11-17 06:09:41gvanrossumsetstatus: open -> closed
resolution: not a bug
stage: resolved
2021-11-17 04:46:11steven.dapranosetnosy: + steven.daprano
messages: + msg406453
2021-11-17 04:28:37nusecreate