Parametrized Unit Tests

rambius rambiusparkisanius at gmail.com
Fri Aug 21 17:17:32 CEST 2015


Hello,

I am running one and the same unit tests that test some web application. I would like to execute them against different servers that may host different instances of the application.

So far I have something like

#!/usr/bin/env python

import unittest

server = ""
user = ""
password = ""

class MyTest(unittest.TestCase):

    def setUp(self):
        global server
        global user
        global password
        self.conn = MyConnection(server, user, password)

    def test_1(self):
        result = run_smth_1(self.conn)
        verify_smth_1(result)

    def test_2(self):
        result = run_smth_2(self.conn)
        verify_smth_2(result)

    def tearDown(self):
        self.conn.close()


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-u', '--user',
                        default=getpass.getuser())
    parser.add_argument('-p', '--password')
    parser.add_argument('-s', '--server')
    args = parser.parse_args()

    server = args.server
    user = args.user
    password = args.password

    unittest.main()


Is there a better a way to pass the server, the user and the password to the test without resolving to global variables?

Although I developed these tests as unit tests they are more of integration tests. Is there an integration testing framework that supports a more convenient passing of test parameters / data?

Thank you in advance for your responses.

Regards
Rambius


More information about the Python-list mailing list