python BDD API测试

发布时间 2023-03-22 21:16:02作者: 筱筱的春天

 

1.    配置内网

确认能登录  https://deopcard.corp.OOOO.com/ui/builds 

Artifacts>set me up>search: pypi>   Get the info as below in resolve tab

在这个文件夹路径下 :Users->username->AppData->Roaming->pip

pip内容

[global]

index-url = https://suehlsu:APA7W98qEsw2WoxQSrQWvunXWT9@deopcard.corp.hkjc.com/artifactory/api/pypi/pypi-remote/simple

trusted-host = deopcard.corp.OOOO.com

 

2. pycharm里面创建项目

配置File -> Settings

 

 

3. 安装插件

File -> Settings -> Plugins 安装Gherkin

 

4. 虚拟环境操作

项目窗口的下面打开Terminal 窗口

pipenv shell 命令,进入虚拟环境

安装 requests 和 behave :

pip install request 

pip install behave

 

5. 写API case

5.1 feature 文件

Feature: Python BDD test

    Scenario: Introduce yourself
        Given What is your name
        When How old are you
        Then Where do you live

 

5.2 写方法文件

import requests
from behave import *
from requests import *


@given('What is your name')
def tellName(context):
    print("My name is A.")
    headers = {"Accept": "application/json,text/javascript, */*", "X-Requested-With": "XMLHttpRequest"}
    url = "http://10.194.109.125/EnvironmentMonitor/ICSQuickLogin"
    response = requests.get(url, headers = headers)
    print(type(response))
    txt = str(response.text)
    print(response.text)
    file = open('response/response.txt', mode = 'a')
    file.write(response.text)
    file.flush()
    file.close()


@when('How old are you')
def tellAge(context):
    print("I'm 100 years old.")


@then('Where do you live')
def tellLocation(context):
    print("I live in SZ.")

 终端窗口执行case命令behave

 

6. 生成报告

安装Allure

带参数执行下面命令,查看报告

behave -f allure_behave.formatter:AllureFormatter -o result ./features

File --> Reload All from Disk

 

7. behave学习地址

https://behave.readthedocs.io/en/stable/