I have a unittest that tests the connection of an url. Individually it works, but I have several urls to test, so I'm trying to call this test module and batch-test them! But I get errors in calling the test function. Could you help me?
test.py:
class TestConnector(unittest.TestCase):
def setUp(self):
[...]
def test_connection(self, url):
conn = Connector(self.user)
self.assertNotEqual(conn.read(url), None)
if __name__ == '__main__':
unittest.main()
Now I want to test several urls, so I created file with them, and try to call the test function:
import test
with open('URL_list.txt') as f:
urls = f.readlines()
suite = unittest.TestLoader().loadTestsFromModule(test.TestConnector)
for url in urls:
unittest.TextTestRunner().run(suite)
And I get this message as many times as urls I have:
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
----------------------------------------------------------------------
What's wrong?
Your test method is ignored because it takes an argument. Test methods never take an argument. This is quite beside the fact that Python won't ever magically pass a local variable name into a function as an argument; you'd have to explicitly pass it in.
Integrate your url loading into the test itself instead:
class TestConnector(unittest.TestCase):
def setUp(self):
[...]
def test_connections(self):
with open('URL_list.txt') as f:
for url in f:
conn = Connector(self.user)
self.assertNotEqual(conn.read(url.strip()), None)
if __name__ == '__main__':
unittest.main()
or create test methods dynamically:
class TestConnector(unittest.TestCase):
def setUp(self):
[...]
def generate_test(url):
def test(self):
conn = Connector(self.user)
self.assertNotEqual(conn.read(url), None)
if __name__ == '__main__':
with open('URL_list.txt') as f:
for i, url in enumerate(f):
test_name = 'test_{}'.format(i)
setattr(TestConnector, test_name, generate_test(url.strip()))
unittest.main()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.