1.执行命令:
python -m unittest E:\lpjx-ui_test\lpjx-web-ui\testcase\web\testcases_pytest\test_XT-0501-002.py
python -m unittest E:\lpjx-ui_test\lpjx-web-ui\testcase\web\testcases_pytest\test_login_DL-001.py
unittest.main(argv=['', 'TestAdd.test_add']
2.组件:
3.TestCase使用方法:
4.断言方法:
5.TestSuite使用: 注意:运行需要借助于TextTestRunner类
6.TestLoader使用:
discover(test_dir, pattern=匹配模式)
参数说明:
- test_dir: 为指定的测试用例的目录,它会搜索该目录的所有文件
- pattern:为匹配模式,如要查找所有的.py格式文件,你就可以写为'test*.py',这样就会搜索test_dir目录下所有以test开头,以py结尾的文件。
7.TextTestRunner使用:
8.参数化:需先导包:ddt,parametrized
- 传单个字符串:
@data("bb")
def test_run_b(self,b):
print(b) #bb
@data(("a","b","c","d"))
@unpack
def test_run_c(self,para1,para2,para3,para4):
print(para1,para2,para3,para4) #a b c d
@data(["a", "b", "c", "d"])
@unpack
def test_run_d(self, para1, para2, para3, para4):
print(para1, para2, para3, para4) #a b c d
- 传几组字符串-data 字典怎么传?
@data(("ae","be"), ("ce","de"))
@unpack
def test_run_e(self, para1, para2): #会生成2条用例
print("test_run_e:"+para1+" "+ para2)
#.test_run_e:ae be
#.test_run_e:ce de
@data(["aa", "bb"], ["cc", "dd"])
@unpack
def test_run_f(self, para1, para2): # 会生成2条用例
print(" test_run_f:"+para1+" "+para2)
#. test_run_f:aa bb
#. test_run_f:cc dd
@data({"aa":"bb"}, {"cc":"dd"}) #字典报错???
@unpack
def test_run_g(self, aa, cc): # 会生成2条用例
print(" test_run_g:"+aa+" "+cc)
- 传几组字符串-parameterized
@parameterized.expand([("ai","bi"), ("ci","di")])
def test_run_i(self, username, password):
print(" test_run_i:" + username + " " + password)
#. test_run_i:ai bi
#. test_run_i:ci di
- 传json文件
{
"user1": {"username": "111", "password": "as"},
"user2": {"username": "222", "password": "ad"},
"user3": {"username": "A320098207", "password": "Cszxqc@2022"}
}
@file_data("E:\demo-selenium-pytest\data\login2.json") #json
def test_run_g2(self, username, password): # 会生成2条用例
print(" test_run_g2:"+username+" "+password)
#. test_run_g2:111 as
#. test_run_g2:222 ad
#. test_run_g2:A320098207 Cszxqc@2022
- 传yaml文件:
user1:
username: "111"
password: "as"
user2:
username: "222"
password: "ad"
user3:
username: "A320098207"
password: "Cszxqc@2022"
@file_data("E:\demo-selenium-pytest\data\login.yaml") #yaml
def test_run_g3(self, username, password): # 会生成2条用例
print(" test_run_g3:" + username + " " + password)
#. test_run_g3:111 as
#. test_run_g3:222 ad
#. test_run_g3:A320098207 Cszxqc@2022
- 传txt文件:
111,as
222,ad
A320098207,Cszxqc@2022
def readtxt():
params = []
file = open("E:\demo-selenium-pytest\data\login3.txt", "r", encoding="utf8")
for line in file.readlines():
params.append(line.strip("\n").split(","))
return params
@ddt
class test_query_userinfo2(unittest.TestCase):
@data(*readtxt()) #txt
@unpack
def test_run_h(self, username, password): #
print(" test_run_h:" + username + " " + password)
#.EE test_run_h:111 as
#. test_run_h:222 ad
#. test_run_h:A320098207 Cszxqc@2022