Reputation: 2194
In Python, how would I pass an argument from the command line to a unittest function?
Here is the code so far… I know it's wrong.
class TestingClass(unittest.TestCase):
def testEmails(self):
assertEqual(email_from_argument, "[email protected]")
if __name__ == "__main__":
unittest.main(argv=[sys.argv[1]])
email_from_argument = sys.argv[1]
Upvotes: 110
Views: 172895
Reputation: 33
I did a little investigation and found a solution like this which for me is excellent:
# conftest.py
import argparse
def pytest_addoption(parser):
'Pytest'
parser.addoption("--name", required=True, help="Name option Pytest")
def parse_args(argv):
'Unit Test'
parser = argparse.ArgumentParser(description='Example argparse usage')
parser.add_argument('input_files', nargs='*', help='Input file path')
parser.add_argument('--name', required=True, help="Name option Unit Test")
return parser.parse_args(argv)
And test_main.py
# test_main.py
import sys
import unittest
from conftest import parse_args
# Transfer sys.args
args = parse_args(sys.argv)
class TestName(unittest.TestCase):
name = None
@classmethod
def setUpClass(cls):
'setup only once'
cls.name = args.name
def test_name(self):
self.assertEqual(self.name, args.name)
def test_upper_name(self):
self.assertEqual(self.name.upper(), args.name.upper())
def test_lower_name(self):
self.assertEqual(self.name.lower(), args.name.lower())
if __name__ == '__main__':
unittest.main(argv=['ignored','-v']) # Allows unittest to take params
So as result, we can run pytest:
pytest ./test_name.py test_name2.py --name Bee
And Unit tests will also work:
python3 ./test_name.py --name Bee
Note: in the Unit test case below, './test_name2.py' will be ignored!
python3 ./test_name.py ./test_name2.py --name Bee
Upvotes: 2
Reputation: 16091
So the doctors here that are saying "You say that hurts? Then don't do that!" are probably right. But if you really want to, here's one way of passing arguments to a unittest test:
import sys
import unittest
class MyTest(unittest.TestCase):
USERNAME = "jemima"
PASSWORD = "password"
def test_logins_or_something(self):
print('username:', self.USERNAME)
print('password:', self.PASSWORD)
if __name__ == "__main__":
if len(sys.argv) > 1:
MyTest.USERNAME = sys.argv.pop()
MyTest.PASSWORD = sys.argv.pop()
unittest.main()
That will let you run with:
python mytests.py myusername mypassword
You need the argv.pop
s, so your command line parameters don't mess with unittest's own...
The other thing you might want to look into is using environment variables:
import os
import unittest
class MyTest(unittest.TestCase):
USERNAME = "jemima"
PASSWORD = "password"
def test_logins_or_something(self):
print('username:', self.USERNAME)
print('password:', self.PASSWORD)
if __name__ == "__main__":
MyTest.USERNAME = os.environ.get('TEST_USERNAME', MyTest.USERNAME)
MyTest.PASSWORD = os.environ.get('TEST_PASSWORD', MyTest.PASSWORD)
unittest.main()
That will let you run with:
TEST_USERNAME=ausername TEST_PASSWORD=apassword python mytests.py
And it has the advantage that you're not messing with unittest's own argument parsing. The downside is it won't work quite like that on Windows...
Upvotes: 171
Reputation: 337
This is my solution:
# your test class
class TestingClass(unittest.TestCase):
# This will only run once for all the tests within this class
@classmethod
def setUpClass(cls) -> None:
if len(sys.argv) > 1:
cls.email = sys.argv[1]
def testEmails(self):
assertEqual(self.email, "[email protected]")
if __name__ == "__main__":
unittest.main()
You could have a runner.py
file with something like this:
# your runner.py
loader = unittest.TestLoader()
tests = loader.discover('.') # note that this will find all your tests, you can also provide the name of the package e.g. `loader.discover('tests')
runner = unittest.TextTestRunner(verbose=3)
result = runner.run(tests
With the above code, you should be to run your tests with runner.py [email protected]
.
Upvotes: 0
Reputation: 99
I have the same problem. My solution is after you handle with parsing arguments using argparse or another way, remove arguments from sys.argv:
sys.argv = sys.argv[:1]
If you need, you can filter unittest arguments from main.parseArgs().
Upvotes: 1
Reputation: 759
Another method for those who really want to do this in spite of the correct remarks that you shouldn't:
import unittest
class MyTest(unittest.TestCase):
def __init__(self, testName, extraArg):
super(MyTest, self).__init__(testName) # calling the super class init varies for different python versions. This works for 2.7
self.myExtraArg = extraArg
def test_something(self):
print(self.myExtraArg)
# call your test
suite = unittest.TestSuite()
suite.addTest(MyTest('test_something', extraArg))
unittest.TextTestRunner(verbosity=2).run(suite)
Upvotes: 42
Reputation: 3892
Even if the test gurus say that we should not do it: I do. In some context it makes a lot of sense to have parameters to drive the test in the right direction, for example:
For me, the use of the environment variable is good enough for this puprose because you do not have to write dedicated code to pass your parameters around; it is supported by Python. It is clean and simple.
Of course, I'm not advocating for fully parametrizable tests. But we have to be pragmatic and, as I said, in some context you need a parameter or two. We should not abouse of it :)
import os
import unittest
class MyTest(unittest.TestCase):
def setUp(self):
self.var1 = os.environ["VAR1"]
self.var2 = os.environ["VAR2"]
def test_01(self):
print("var1: {}, var2: {}".format(self.var1, self.var2))
Then from the command line (tested on Linux)
$ export VAR1=1
$ export VAR2=2
$ python -m unittest MyTest
var1: 1, var2: 2
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Upvotes: 17
Reputation: 1350
If you want to use steffens21's approach with unittest.TestLoader
, you can modify the original test loader (see unittest.py
):
import unittest
from unittest import suite
class TestLoaderWithKwargs(unittest.TestLoader):
"""A test loader which allows to parse keyword arguments to the
test case class."""
def loadTestsFromTestCase(self, testCaseClass, **kwargs):
"""Return a suite of all tests cases contained in
testCaseClass."""
if issubclass(testCaseClass, suite.TestSuite):
raise TypeError("Test cases should not be derived from "\
"TestSuite. Maybe you meant to derive from"\
" TestCase?")
testCaseNames = self.getTestCaseNames(testCaseClass)
if not testCaseNames and hasattr(testCaseClass, 'runTest'):
testCaseNames = ['runTest']
# Modification here: parse keyword arguments to testCaseClass.
test_cases = []
for test_case_name in testCaseNames:
test_cases.append(testCaseClass(test_case_name, **kwargs))
loaded_suite = self.suiteClass(test_cases)
return loaded_suite
# call your test
loader = TestLoaderWithKwargs()
suite = loader.loadTestsFromTestCase(MyTest, extraArg=extraArg)
unittest.TextTestRunner(verbosity=2).run(suite)
Upvotes: 6
Reputation: 20794
Unit testing is meant for testing the very basic functionality (the lowest level functions of the application) to be sure that your application building blocks work correctly. There is probably no formal definition of what does that exactly mean, but you should consider other kinds of testing for the bigger functionality -- see Integration testing. The unit testing framework may not be ideal for the purpose.
Upvotes: -4