Reputation:
I have 2 files:
test_1.py:
import unittest
class TestMe(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.name = "test"
cls.password = "1234"
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_user_pass(self):
print(self.name)
print(self.password)
if __name__ == '__main__':
unittest.main()
test_2.py:
import unittest
import test_1
import sys
a = sys.argv
if a[1] == '2':
suite=unittest.TestLoader().loadTestsFromModule(test_1)
unittest.TextTestRunner(verbosity=2).run(suite)
I want to pass the argument to test_1(unittes module) but I need this argument to setUpClass. How can I do that?
Thanks!!!
Upvotes: 3
Views: 182
Reputation: 61
Try this...
test_1.py:
import unittest
from test_2 import b
class TestMe(unittest.TestCase):
e = b
@classmethod
def setUpClass(cls):
cls.name = "test"
cls.password = "1234"
cls.parameter = cls.e
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_user_pass(self):
print(self.name)
print(self.password)
print(self.parameter)
if __name__ == '__main__':
unittest.main()
test_2.py:
import unittest
import test_1
import sys
a = sys.argv
b = ""
if a[1] == '2':
b = a[1]
suite = unittest.TestLoader().loadTestsFromModule(test_1)
unittest.TextTestRunner(verbosity=2).run(suite)
I hope this will help you.
Upvotes: 1
Reputation: 3306
There are several of doing this.
The easiest choice would be to include the following in all your scripts.
import sys
some_arg = sys.argv[1]
You will be able to access the argument you put into your main in every file you go after.
The second option would be to use this as a function, typically, you could do...
def build_test_class(arg):
Class TestMe(unittest.TestCase):
... do stuff ...
return TestMe
Here, you would have your argument to use with your class.
But there must be another way of doing what you're trying to do. I think this is an XY Problem.
Upvotes: 0