5

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

2
  • you can do it via bash or you use use Popen and open the unittest in a threadpool Commented Sep 19, 2014 at 11:23
  • It works pretty good vie bash. I wonder whether this solution is "nice". Commented Sep 19, 2014 at 11:27

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I got some problem with passing arguments
Interesting. In response to this comment and the follow on post from Adam, I'm wondering if there is a bug in Python2.7 that was fixed in python 3.3 (or earlier). If you open the file where the problem occurs (multiprocessing/process.py), you can put a str() around the e.arg[0] to get it to work. sys.stderr.write(str(e.args[0]) + '\n')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.