Erben Mo
Erben Mo

Reputation: 3614

how to unit test a python process

I am trying to write a unit test for a python process. I want to run the process for a few seconds and then do an assert to make sure that an instance variable of that process equals to the expected value.

class MyProcess(multiprocessing.Process):
    def run():
       ......

class TestMultiProcess(unittest.TestCase):

    def testStuff(self):
        process = MyProcess()
        process.start()
        time.sleep(20)
        process.exit.set()
        process.join()

        assert(process.ivar == 42)

The problem is in the last line. Because it is a process not a thread, I can't get the value of ivar directly (always equals to 0). I am wondering what is the preferred way to get that ivar?

Upvotes: 2

Views: 2659

Answers (1)

jfs
jfs

Reputation: 414079

The preferred way to unit test a code that is run using multiprocessing is to test individual functions directly if possible.

To get a value from another process, you could send it: mp.Pipe(), mp.Queue() or you could use a shared value: mp.Value().

Upvotes: 2

Related Questions