Testing a function that can return non-deterministic results with Python unittest

I am writing a small task scheduler in Python. A planner can be provided with a series of calls and dependencies, and they must launch the calling calls, making sure that none of its predecessors are executed.

I am trying to follow a test-based approach, and I ran into the problem of handling test dependencies. My test code is as follows:

def test_add_dependency(self):
    """Tasks can be added with dependencies"""
    # TODO: Unreliable test, may work sometimes because by default, task
    #       running order is indeterminate.
    self.done = []
    def test(id):
        self.done.append("Test " + id)
    s = Schedule()
    tA = Task("Test A", partial(test, "A"))
    tB = Task("Test B", partial(test, "B"))
    s.add_task(tA)
    s.add_task(tB)
    s.add_dependency(tA, tB)
    s.run()
    self.assertEqual(self.done, ["Test B", "Test A"])

The problem is that this test (sometimes) worked even before I added the dependency handling code. This is due to the fact that the specification does not indicate that tasks should be performed in a specific order. Thus, the correct order is a legitimate choice, even if dependency information is ignored.

, "" ? , , " ".

+5
4

:

-, ( , RNG PRNG ..). , , :

from itertools import permutations
def test_add_dependency(self):
    """Tasks can be added with dependencies"""
    for p in permutations("AB"):
        self.done = []
        def test(id):
            self.done.append("Test " + id)
        s = Schedule(threads=1)
        tasks = {id: Task("Test " + id, partial(test, id)) for id in "AB"}
        s.add_task(tasks['A'])
        s.add_task(tasks['B'])
        s.add_dependency(tasks[p[0]], tasks[p[1]])
        s.run()
        self.assertEqual(self.done, ["Test " + p[1], "Test " + p[0]])

, Schedule add_dependency, ( ), .

+1

, , .

, , , , . , , , , , , .

; , . , . , ; - .

, , , .

+2

- , Schedule ( , ) , unit test.

.


, ...

, "" ?

..., , "", , . , , , , , , .; -)

, ?

"meta" , "meta"? ...

, , , , , "", "". , .

+1

, .

, , , : , , , .

on the other hand, a more valuable test could be to confirm the presence (or absence) of tasks in the results without asserting anything about their position: "is in the set" vs "is in the position of the array"

+1
source

All Articles