当前位置:网站首页>Advanced chapter of postman interface testing tool - how to skillfully set assertions

Advanced chapter of postman interface testing tool - how to skillfully set assertions

2021-08-10 08:14:28 Test Mengmeng

postman Request to publish topic assertion

var jsonData = pm.response.json();



pm.test("success value is true", function () {
    



        pm.expect(jsonData["success"]).to.eql(true);

});



pm.test("topic id value is not null",function(){
    

    var topic_id = jsonData['topic_id'];

    //  More assertive operations , see http://www.chaijs.com/api/

bdd/

   pm.expect(topic_id).to.not.eql(null);

})

Interface Association automation how to do ?

1. Interface Association :2 or 3 More than one value is directly transmitted through a certain data as a hub .

Solutions :

1. The first interface gets the server response result , Extract the value from the response result , Set as variable .

2. The following interface can directly call the variable value .

Pre-request-script

Perform the operation before sending the request , For example, you can set some variables here

//  Send the requested script 



pm.environment.set("title", "1111111111111111111111111111");

pm.environment.set("tab", "ask");

pm.environment.set("content", "22222222222222222222222222");

 Insert picture description here

Variables can be called in scripts , Or to { { Variable name }} The way

 Insert picture description here

In other requests 【Tests】 Script , Get variable , No more use { { Variable name }} Method call . To use the method of getting variables to call .

var jsonData = pm.response.json();  



var data = jsonData['data']; 



//  Get the environment variable set in the publishing topic  title

var title = pm.environment.get("title");  

pm.test("title  The value of should be "+title, function () {
    

        pm.expect(data["title"]).to.eql(title);

});



//  Get the environment variable set in the publishing topic content

var content = pm.environment.get("content");
pm.test("content  The value of should be "+content, function () {
    

    pm.expect(data["content"]).to.eql(content);

});

 //  Get the environment variable set in the publishing topic tab

var tab = pm.environment.get("tab"); 

pm.test("tab  The value of should be "+tab, function () {
    

    pm.expect(data["tab"]).to.eql(tab);

});

Runner Run the results in

 Insert picture description here

Data driven testing

The core : Conduct automated testing according to the content of the data file

1 Create test data file data.csv

Solve the problem of Chinese garbled code :

Notepad open data.csv file ,【 file 】–【 Save as 】 choice utf8 code --【 preservation 】
 Insert picture description here

2 Design script

Variables are used in scripts , Variables should be related to csv The fields of the file are saved to .

 Insert picture description here

3 The assertion part of the series

 Insert picture description here

//  Get variable 

var except_val = pm.variables.get("except_val");

pm.test("error_msg value should be "+except_val, function () {
    

    var jsonData = pm.response.json();

    pm.expect(jsonData['error_msg']).to.eql(except_val);

});

Be sure to save the script after editing .

4 Runner Run in

 Insert picture description here

perform

 Insert picture description here

python Script

1 Interface return value

import requests



"""





"""





base_url = "http://39.107.96.138:3000/api/v1/"



testdata = {
    

        "accesstoken":"49b2e830-4305-475d-b6b5-52287cc

5daaa",

        "title":"2313131231231232",

        "tab":"ask",

        "content":"xxxxxxxxxxxxx"

    }

def test_new_topic():

    """

     Test release topics 

    :return:

    """

    url = base_url+'topics'

    r = requests.post(url,json=testdata)

    jsonData = r.json()



    assert r.status_code == 200



    assert jsonData['success']

    print("test_new_topic, topicid:",jsonData['topic_id'])



    assert jsonData['topic_id'] is not None



    return jsonData['topic_id']



def test_topic_detail():

    topic_id = test_new_topic()

    print("test_topic_detail topicid:",topic_id)

    url = base_url+"/topic/"+topic_id

    testparmas = {
    

        'mdrender':'false'

    }

    r = requests.get(url,params=testparmas)

    print("rjson :",r.json())



    jsonData = r.json()

    data = jsonData['data']



    assert data['title'] == testdata['title']

    assert data['tab'] == testdata['tab']

    assert data['content'] == testdata['content']



def test_update_topic():

    pass

2 Use fixture

adopt @pytest.fixture Pass parameters

@pytest.fixture

scope: Scope

autouse: Whether to load automatically

name : Quote name

parmas: Parameters

ids: Execute display function name suffix

import pytest

import requests



base_url = "http://39.107.96.138:3000/api/v1/"

testdata = {
    

        "accesstoken":"49b2e830-4305-475d-b6b5-52287c

c5daaa",

        "title":"2313131231231232",

        "tab":"ask",

        "content":"xxxxxxxxxxxxx"

    }



@pytest.fixture(scope="module",autouse=True,name="topic_id")

def newtopic():

    url = base_url + 'topics'

    r = requests.post(url, json=testdata)

    jsonData = r.json()



    return jsonData['topic_id']





def test_update_topic(topic_id):

    """

     Edit topic 

    :return:

    """

    print("test_update_topic",topic_id)



def test_collect_topic(topic_id):

    """

     Collect topics 

    :return:

    """

    print("test_collect_topic",topic_id)





def test_reply_topic(topic_id):

    """

     Reply to the topic 

    :return:

    """

    print("test_reply_topic",topic_id)

Homework 1

The above code Three api use python How to achieve .

1 csv Data driven

Be careful csv Path to file

import pytest

import os

import csv

import requests





"""

 Use csv Data driven testing of the data in 

"""



dir=os.path.dirname(os.path.dirname( os.path.dirname(__file__)))



csvfile = os.path.join(dir,'data/data.csv')



csvdata = []

with open(csvfile,encoding='utf8') as file:

    filedata= csv.reader(file)

    next(filedata)

    for x in filedata:

        csvdata.append(x)



print(csvdata)

"""

 [['0418178a-b80c-4e15-aa8f-bab03a3491cb', '1111111111', 'ask', '22222222222', ' FALSE accessToken'], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '', 'ask', '2222222222', ' The title cannot be empty '], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '1', 'ask', '2222222222', ' Too many or too few words in the title '], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '1111111111', '', '2222222222', ' You must select a section '], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '1111111111', 'ask', '', ' The content cannot be empty ']]

  ||    Data format conversion 

  \/

[{
    "token":"0418178a-b80c-4e15-aa8f-bab03a3491cb","tab":"ask"},{
    ....}]

"""





@pytest.fixture(params=csvdata)

def data(request):

    return request.param



base_url = "http://39.107.96.138:3000/api/v1/"

def test_topics(data):

    url = base_url + 'topics'

    testdata={
    

        "accesstoken": data[0],

        "title": data[1],

        "tab": data[2],

        "content": data[3]

    }

    r = requests.post(url, json=testdata)

    jsonData = r.json()



    assert jsonData['error_msg'] == data[4]

Homework 2

Data format conversion

Reference resources Data format conversion

http://docs.python.org/zh-cn/3.7/tutorial/inputoutput.html#saving-structured-data-with-json

2 Excel As data driven

import pytest

import os

import requests

from openpyxl import load_workbook

from openpyxl.worksheet.worksheet import Worksheet





"""

 Use Excel Data driven testing of the data in 

"""



dir=os.path.dirname(os.path.dirname( os.path.dirname(__file__)))

excelfile = os.path.join(dir,'data/data.xlsx')



workbook = load_workbook(excelfile)worksheet:

Worksheet = workbook['topic_data']

exceldata = []

for row in worksheet.iter_rows(min_row=2,max_row=6,min_col=1,max_col=5,values_only=True):

    # print(row)

    exceldata.append(row)







"""

 [['0418178a-b80c-4e15-aa8f-bab03a3491cb', '1111111111', 'ask', '22222222222', ' FALSE accessToken'], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '', 'ask', '2222222222', ' The title cannot be empty '], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '1', 'ask', '2222222222', ' Too many or too few words in the title '], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '1111111111', '', '2222222222', ' You must select a section '], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '1111111111', 'ask', '', ' The content cannot be empty ']]

  ||    Data format conversion 

  \/

[{
    "token":"0418178a-b80c-4e15-aa8f-bab03a3491cb","tab":"ask"},{
    ....}]

"""





@pytest.fixture(params=exceldata)

def data(request):

    return request.param



base_url = "http://39.107.96.138:3000/api/v1/"



def test_topics(data):

    url = base_url + 'topics'

    testdata={
    

        "accesstoken": data[0],

        "title": data[1],

        "tab": data[2],

        "content": data[3]

    }

    r = requests.post(url, json=testdata)

    jsonData = r.json()



    assert jsonData['error_msg'] == data[4]

Last : Welcome everyone to follow the official account :【 Sad spicy bar 】, Take one 300 page pdf Document Python Summary of the core knowledge of automation test engineer !

Most of the information in the official account is the knowledge point that the interviewer will ask when interviewed , It also includes a lot of common knowledge in the testing industry , It includes basic knowledge 、Linux necessary 、Shell、 The principles of the Internet 、Mysql database 、 Special topic of bag capturing tools 、 Interface testing tool 、 Test advanced -Python Programming 、Web automated testing 、APP automated testing 、 Interface automation testing 、 Testing advanced continuous integration 、 Test architecture development test framework 、 Performance testing 、 Safety test, etc. .

If you have a lot of confusion in the test , Then the software testing technology exchange group I created will be a useful community for you to contact good teachers and friends , Peers may bring you some practical help and breakthrough . Group :902061117 You also want to know how your peers are getting rich !

If it helps you a little , Everyone 「 give the thumbs-up 」 Is the biggest driving force of Xiaobian's creation , See you in the next article !

Good article recommends :

Package as 1 Years working experience as a test engineer , My advice to him before the interview is as follows

interviewer : Work for three years , And a preliminary test ? I'm afraid the title of your software test engineer should be enclosed in double quotation marks …

“ One year in the job , The automated software test hired by high salary was discouraged .”

The man who leaves work on time , Promoted before me …

Summary of automated testing
 Insert picture description here

版权声明
本文为[Test Mengmeng]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/08/20210810081223788y.html

随机推荐