ddt
Python は、データ駆動型を実装するために特別に設計されたサードパーティ パッケージです。
-
対応するddtファイルをインストールします
pip install ddt
-
内容/モジュール
- データ - 格納データ形式: 辞書タプルリスト
- unpack - データのアンパックに使用され、対応するデータが自動的に個別に分割されます。
- fila_data - 保存データ形式: json yaml (辞書形式で表示されます)
-
使用
- インポートパッケージ
from ddt import ddt, data,file_data,unpack
- 対応するクラスを装飾する
@ddt class TestLogin(unittest.TestCase): def...
-
ユースケース実行用のテストデータを準備する
ユーザーログインをシミュレートします。ユーザー名とパスワードのみが必要です。ここでは一部のコードスニペットのみが示されています~
@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) ...
上記は ddt でのデータ モジュールの実装です。以下の file_data 操作を使用してみましょう。
- 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"])
要約する
file_data は json ファイルと yaml ファイルのデータを取得できます。データは別の json ファイルに配置する必要があり、json/yaml ファイルの内容は def 関数のパラメーターの数に対応する必要があります。
data data は比較的直感的で、py ファイルに直接書き込まれます。