Suprido
Suprido

Reputation: 533

Python unit tests multiple threads

I'm testing an web application and I've already written some tests using unittest. There are some necessary steps like authorization, exchanging data which are being tested and so on. Now I want to test if everything works fine for more then one client. Actually I'd like to call the same test for every client in seperate thread, gather all return codes and print result. My question is how to create such threads in python? (my ad hoc solution in bash spawns multiple python processes)

Let's consider an example:

import unittest

class Test(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testName(self):
        pass

if __name__ == "__main__":
    unittest.main()

thread.start_new_thread(unittest.main) #something like this does not work

Upvotes: 5

Views: 4413

Answers (1)

JasonAUnrein
JasonAUnrein

Reputation: 156

Google around, there are a number of precanned options. Nose seems to be a common one.

Otherwise for one of my projects, this worked for my in python 3.3

if __name__ == "__main__":
    from multiprocessing import Process
    procs=[]
    procs.append(Process(target=unittest.main, kwargs={'verbosity':2}))
    procs.append(Process(target=unittest.main, kwargs={'verbosity':2}))
    for proc in procs:
        proc.start()
    for proc in procs:
        proc.join()

If you only want to run specific tests, then do similar to above but use Suites from unittest.

Upvotes: 3

Related Questions