From the blog: Shanghai-leisurely
Original address: http://www.cnblogs.com/yoyoketang/tag/unittest/
Objective
In the design of use cases, some use cases are only parameter data input is not the same, such as login this function, operation process but the same. If the use cases repeat the write operation process will increase the amount of code, corresponding to this multi-set of data test cases, can be used in data-driven design patterns, a set of data corresponding to a test case, use case automatic loading generation.
First, the Environment preparation
1. Install the DDT module, open cmd input pip install DDT online installation
>>pip Install DDT
Second, the data-driven principle
1. Test data is a list type of multiple dictionaries
2. Test the class before adding the modified @ddt.ddt
3.case Pre-Modified @ddt.data ()
4. Post-run use cases are automatically loaded into three separate use cases
5. Test results:
Testing started at 21:51 ...
start!
{' username ': ' selenium\xe7\xbe\xa4 ', ' PSW ': ' 232607095 '}
end!
start!
{' username ': ' python\xe7\xbe\xa4 ', ' PSW ': ' 226296743 '}
end!
start!
{' username ': ' appium\xe7\xbe\xa4 ', ' PSW ': ' 512200893 '}
end!
Three, selenium case
1. Read data from the previous encapsulated Excel method, as test test Selenium2+python Automation 58-read Excel data (XLRD)
2. Make some changes based on the previous written login, test parameters read the data in Excel Selenium2+python Automation 48-Login method (parametric)
3. The code reference is as follows
# test Data
TestData = Data.dict_data ()
Print TestData
@ddt. DDT
Class Bolg (UnitTest. TestCase):
U ' Login blog '
def setUp (self):
Self.driver = Webdriver. Firefox ()
url = "Https://passport.cnblogs.com/user/signin"
Self.driver.get (URL)
Self.driver.implicitly_wait (30)
def login (self, username, PSW):
U ' ' here wrote a login method, account number and password parameterization '
self.driver.find_element_by_id ("Input1"). Send_keys (username)
self.driver.find_element_by_id ("Input2"). Send_keys (PSW)
self.driver.find_element_by_id ("Signin"). Click ()
Time.sleep (3)
def is_login_sucess (self):
U "" to determine whether to get to the login account name '
Try
Text = self.driver.find_element_by_id ("Lnk_current_user"). Text
Print text
Return True
Except
Return False
@ddt. Data (*testdata)
def test_login (self, data):
U ' Login Case reference '
Print ("Current test data%s"%data)
# Call the Login method
Self.login (data["username"], data["password"])
# Judging Results
result = Self.is_login_sucess ()
Self.asserttrue (Result)
def tearDown (self):
Self.driver.quit ()
if __name__ = = "__main__":
Unittest.main ()
Selenium2+python Automation 59-data-driven (DDT) "reprint"