import unittest class MyTest(unittest.TestCase): def __init__(self): self.test_login() self.test_query() def test_login(self): print('test_login self:',id(self)) def test_query(self): print('test_query self:',id(self)) if __name__ == '__main__': MyTest()
RESULTS: instructions test_login and test_query of self is the same
test_login self: 13940664 test_query self: 13940664
Inheritance unittest.TestCase
import unittest class MyTest(unittest.TestCase): def setUp(self): print('setup self:',id(self)) def tearDown(self): print('tearDown self:',id(self)) def test_login(self): print('test_login self:',id(self)) def test_query(self): print('test_query self:',id(self)) if __name__ == '__main__': unittest.main()
Run Results: Description of self test_query test_login and is not the same, even in the same class
setup self: 17905872 test_login self: 17905872 tearDown self: 17905872 setup self: 17905648 test_query self: 17905648 tearDown self: 17905648
The reasons for differences in the results above, do not go into here, you can go study at unittest source of interest.
Above all the bedding, we need to consider here is that the interface automated testing framework if the association?
For example, we need a system to log all subsequent requests must pass to get the token login
Since unittest in, even if it is the same class, each self use case is not the same, of course, use cases can not be logged in as self set up a property, self.token = xxx, because other use cases simply do not get
import unittest class MyTest(unittest.TestCase): def setUp(self): print('setup self:',id(self)) def tearDown(self): print('tearDown self:',id(self)) def test_login(self): print('test_login self:',id(self)) self.token = 'https://www.cnblogs.com/uncleyong/p/10530261.html' def test_query(self): print('test_query self:',id(self)) print(self.token) if __name__ == '__main__': unittest.main()
An error
Traceback (most recent call last): File "F:/uncleyong/test.py", line 16, in test_query print(self.token) AttributeError: 'MyTest' object has no attribute 'token'
Scheme I: Method define dependencies
If test_add_b rely test_add_a, test_add_a can not be changed at the beginning of test, such as to add_a, unittest will not run add_a,
test_add_b call add_a, add_a it will run as part of test_add_b, it is necessary to say whether success in add_a, if not successful, it will not execute the code behind test_add_b, test_add_b this use case execution is a failure.
However, this somewhat cumbersome, not necessarily only a py file a class, use case and not necessarily in the py file, so that each class should write again, it is best to save as a global token
import unittest class MyTest(unittest.TestCase): def setUp(self): print('setup self:',id(self)) def tearDown(self): print('tearDown self:',id(self)) def login(self): print('test_login self:',id(self)) self.token = 'https://www.cnblogs.com/uncleyong/p/10530261.html' def test_query(self): print('test_query self:',id(self)) self.login() print(self.token) if __name__ == '__main__': unittest.main()
result
setup self: 17905032 test_query self: 17905032 test_login self: 17905032 https://www.cnblogs.com/uncleyong/p/10530261.html tearDown self: 17905032
Replaced by a static method, we need to return
import unittest class MyTest(unittest.TestCase): def setUp(self): print('setup self:',id(self)) def tearDown(self): print('tearDown self:',id(self)) @staticmethod def login(): print('test_login self') token = 'https://www.cnblogs.com/uncleyong/p/10530261.html' return token def test_query(self): print('test_query self:',id(self)) token = self.login() print(token) if __name__ == '__main__': unittest.main()
result
setup self: 17905032 test_query self: 17905032 test_login self https://www.cnblogs.com/uncleyong/p/10530261.html tearDown self: 17905032
Other classes may also be used
import unittest
class MyTest(unittest.TestCase):
def setUp(self):
print('setup self:',id(self))
def tearDown(self):
print('tearDown self:',id(self))
@staticmethod
def login():
print('test_login self')
token = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
return token
def test_query(self):
print('test_query self:',id(self))
token = self.login()
print(token)
class MyTest2(unittest.TestCase):
def setUp(self):
print('setup self:',id(self))
def tearDown(self):
print('tearDown self:',id(self))
def test_delete(self):
print('test_query self:',id(self))
token = MyTest.login()
print(token)
if __name__ == '__main__':
unittest.main()
result
setup self: 17836464 test_query self: 17836464 test_login self https://www.cnblogs.com/uncleyong/p/10530261.html tearDown self: 17836464 setup self: 17835792 test_query self: 17835792 test_login self https://www.cnblogs.com/uncleyong/p/10530261.html tearDown self: 17835792
Question: If a support concurrent use cases and a use case b rely token login, the use cases get a first call log on to the token, but due to network reasons, did not lead to a request sent to the server
At this time, there is obtained with the embodiment b token (the token prior to failure), a use case might fail performed
Log calls, pass different login name
Option II: define a singleton instance variables
Single-mode embodiment, only one instance, the test login time, to add an instance variable in this example, the interface to the subsequent dependent token log, to obtain directly from the instance variable.
Option Three: Write file
When the test log, put a token to get written to the file, follow interface depends token login, you can directly read from the file.