ddt implements automated test data-driven (unittest + selenium)

ddt

Python is a third-party package specially designed to implement data-driven

  1. Install the corresponding ddt file

    pip install ddt

  2. Contains/Module

    • data - storage data format: dictionary tuple list
    • unpack - used for data unpacking, the corresponding data is automatically divided separately
    • fila_data - storage data format: json yaml (read it will be displayed in dictionary format)
  3. use

    • import package
    from ddt import ddt, data,file_data,unpack
    
    • Decorate the corresponding class
    @ddt
    class TestLogin(unittest.TestCase):
    	def...
    
    • Prepare test data for use case execution

      Simulate user login, only user name and password are required, only some code snippets are shown here~

      @ddt
      class TestLogin(unittest.TestCase):
      	# 方法一
      	@data({
              
              "username":"yyy","password":"111111"})
      	@unpack
          def test_login_success(self,**paras):
      		self.driver.find_element(By.ID,"username").send_keys(paras["username"])
      		self.driver.find_element(By.ID,"password").send_keys(paras["password"])
      
      	# 方法二 (适用于多个字典数据 单个用例操作)
      	datas = [{
              
              "username":"yyy","password":"111111"},{
              
              "username":"hhh","password":"123456"}]
      	@data(*datas)
      	@unpack
          def test_login_success(self,**paras):
      		self.driver.find_element(By.ID,"username").send_keys(paras["username"])
      		self.driver.find_element(By.ID,"password").send_keys(paras["password"])
      		
      	# 方法三 (data数据为字典类型,参数要和key一样)
      	@data({
              
              "username":"yyy","password":"111111"})
      	@unpack
          def test_login_success(self,username,password):
      		self.driver.find_element(By.ID,"username").send_keys(username)
      		self.driver.find_element(By.ID,"password").send_keys(password)
      
      	# 方法四 (适用于多个数据单个用例操作,这里数据类型为列表或者元祖)
      	@data(["yyy","111111"],["hhh","111111"])
      	@unpack
          def test_login_success(self,username,password):
      		self.driver.find_element(By.ID,"username").send_keys(username)
      		self.driver.find_element(By.ID,"password").send_keys(password)
      	
      	...
      
      

      The above is the implementation of the data module in ddt, let's use the file_data operation below:

      - caseName: test_login_success
        username: yyy
        password: 111111
      
      @ddt
      class TestLogin(unittest.TestCase):
      	@file_data("yaml文件的路径/login.yaml")
      	@unpack
          def test_login_success(self,**paras):
      		self.driver.find_element(By.ID,"username").send_keys(paras["username"])
      		self.driver.find_element(By.ID,"password").send_keys(paras["password"])
      

Summarize

file_data can get the data of json file and yaml file; the data needs to be put into a separate json file, and the content in the json/yaml file needs to correspond to the number of parameters in the def function;

data data is relatively intuitive, directly written in the py file;

Guess you like

Origin blog.csdn.net/weixin_46761622/article/details/129032957
Recommended