pytest參數化和執行

參數化有兩種

1、使用pytest.fixture

1、使用pytest.fixture標記數據

#定義數據,數據必須是列表
undata = [{"username":"xue111","password":'123456'},{"username":"xue222","password":'234567'},{"username":"xue333","password":'345678'},{"username":"xue444","password":'456789'}]

def id(fixture_vaule):
    t = fixture_vaule
    return "username,password{0}".format(t)


#fixture標記函數 undata 表示需要標記的數據列表
@pytest.fixture(params=undata,ids=id)
def username(request):
    return request.param

2、根據標記方法名調用參數

    #username,表示使用哪個fixture標記的函數
    def test_sign_up(self,username):

        url = "http://192.168.3.7:8080/admin/register"
        body = {
  "email": "593971579@qq.com",
  "icon": "",
  "nickName": "",
  "note": "",
  "password": username["password"],
            "username": username["username"]
                   }



        r = requests.post(url = url, json=body)
        print(r.request.body)
        assert True == r.text.__contains__("200")

3、執行并驗證


image.png

使用pytest.mark.parametrize

@pytest.mark.parametrize('username,password',[('admin','123456'),('wwwwww','123456'),('eeeeee','123456')])
    def test_sign_up_2(self, username,password):
        url = "http://192.168.3.7:8080/admin/register"
        body = {
            "email": "593971579@qq.com",
            "icon": "",
            "nickName": "",
            "note": "",
            "password": "123456",
            "username": "xue000"
        }
        body["username"] = username
        body["password"] = password
        r = requests.post(url=url, json=body)
        print(r.request.body)
        print(r.text)
        print(r.json()["data"])
        assert r.json()["data"] == None

用例執行

pytest -v

執行整個項目中的測試用例,并顯示詳細信息

pytest -v 路徑

執行某個包下邊的測試用例

pytest -v 文件路徑

執行某個測試文件中的測試用例

pytest -v 文件路徑::測試類

執行某個測試類中的測試用例

pytest -v 文件路徑::測試類::測試方法

執行某個測試用例

使用pytest.mark標記測試用例并執行

1、使用pytest.mark打標記

image.png

2、執行

image.png

3、執行多個標記

image.png
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容