Skip to content

tests pass/fail randomly #127

@DavidAnderegg

Description

@DavidAnderegg

The following snipped sometimes fails and sometimes passes

import unittest

class DummyVariable:
    def __init__(self) -> None:
        self.flag = False

    def activate_flag(self):
        if self.flag:
            raise ValueError('Already flagged')
        self.flag = True


variable = DummyVariable()

class TestVariable(unittest.TestCase):

    def test_1(self):
        variable.activate_flag()

    def test_2(self):
        variable.activate_flag()

This is due to the consistent state of variable. As far as I understand, testflo executes these tests in different subprocesses based on randomization. If both tests happen to be run in the same process, the second test fails. If both tests happen to be run on different processes, the second test passes.

If I specify the -i, --isolated flag which forces testflo to run each test in its own subprocess, both tests pass all the time.

If I run this tests with python -m unittest, the second test fails all the time.

You could argue the snipped above is bad test design and I would agree but I think it is very dangerous to have such random behavior in a test setup. I think it should either fail or pass in a consistent way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions