pyresttest环境安装完毕之后,进行如下操作,快速入门pyresttest。
参考:【PYRESTTEST】PYRESTTEST简述及安装

第一步:创建一个简单的REST服务

1、进行如下命令clonepyresttest项目:

git clone https://github.com/svanoort/pyresttest.git

2、安装运行Rest服务的依赖包 (Django and Django Tastypie):

sudo pip install 'django >=1.6, <1.7' django-tastypie==0.12.1

3、打开一个终端执行如下命令启动一个rest服务:

cd pyresttest/pyresttest/testapp
python manage.py testserver test_data.json

如果启动出现如下错误:

Traceback (most recent call last):
  File "/usr/bin/django-admin.py", line 2, in <module>
    from django.core import management
  File "/usr/lib64/python2.6/site-packages/django/core/management/__init__.py", line 68
    commands = {name: 'django.core' for name in find_commands(__path__[0])}

是由于版本不兼容问题导致,可以使用如下命令解决:

sudo pip uninstall -y django django-tastypie
sudo pip install 'django >=1.6, <1.7' django-tastypie==0.12.1

执行完上述命令,重新执行启动东rest服务命令,服务启动成功会提示:http://localhost:8000

重新打开一个新的终端命令控制台执行如下命令:

curl -s http://localhost:8000/api/person/2/ | python -m json.tool

如果所有都正常,可以看到类似于下面的响应信息:

{
    "first_name": "Leeroy", 
    "id": 2, 
    "last_name": "Jenkins", 
    "login": "jenkins", 
    "resource_uri": "/api/person/2/"
}

至此一个简单的Rest服务搭建完毕。

第二步: 编写一个简单的smoke测试

重新打开一个窗口,创建一个文件名为:test.yaml,文件内容如下:

---
- config:
    - testset: "Quickstart app tests"

- test:
    - name: "Basic smoketest"
    - url: "/api/people/"

然后执行命令:

resttest.py http://localhost:8000 test.yaml

你可能注意到,上述文件中的API接口“/api/people/”是错误的会导致测试失败,控制台将显示unexpected 404且报告测试的名称。但在报告最后汇总信息,测试组默认是Default,我们可以通过group字段自定义自己的组。调整之后的test.yaml文件内容如下:

---
- config:
    - testset: "Quickstart app tests"

- test:
    - group: "Quickstart"
    - name: "Basic smoketest"
    - url: "/api/person/"

重新执行命令:resttest.py http://localhost:8000 test.yaml,测试通过。

第三步: 使用不同的请求方法

Rest API有很多方法,如常用地的POST、GET、PUT、Delete等。

示例一:通过PUT添加用户,然后测试该用户是否添加:

文件putDemo.yaml内容如下:

---
- config:
    - testset: "Quickstart app tests"

- test:
    - group: "Quickstart"
    - name: "Basic smoketest"
    - url: "/api/person/"

- test:
    - group: "Quickstart"
    - name: "Create a person"
    - url: "/api/person/10/"
    - method: "PUT"
    - body: '{"first_name": "Gaius","id": 10,"last_name": "Baltar","login": "baltarg"}'
    - headers: {'Content-Type': 'application/json'}

- test:
    - group: "Quickstart"
    - name: "Make sure Mr Baltar was added"
    - url: "/api/person/10/"

第三个测试在运行时可能会出现问题,因为只有当Balter在数据库存在时,该测试才能通过。那是不是可以在执行第三个测试之前,做一个测试判断Balter是否存在呢?调整之后的文件内容如下:

---
- config:
    - testset: "Quickstart app tests"

- test:
    - group: "Quickstart"
    - name: "Make sure Mr Baltar ISN'T there to begin with"
    - url: "/api/person/10/"
    - expected_status: [404]

- test:
    - group: "Quickstart"
    - name: "Basic smoketest"
    - url: "/api/person/"

- test:
    - group: "Quickstart"
    - name: "Create a person"
    - url: "/api/person/10/"
    - method: "PUT"
    - body: '{"first_name": "Gaius","id": 10,"last_name": "Baltar","login": "baltarg"}'
    - headers: {'Content-Type': 'application/json'}

- test:
    - group: "Quickstart"
    - name: "Make sure Mr Baltar is there after we added him"
    - url: "/api/person/10/"

现在第一个测试会失败:因为用户已经添加到数据库中,能正确响应,状态码为200,不符合预期结果。为了能让测试形成闭环,可以在添加用户之后,然后删除,让当前文件中的数据形成一个闭环。调整之后的文件内容如下:

---
- config:
    - testset: "Quickstart app tests"

- test:
    - group: "Quickstart"
    - name: "Make sure Mr Baltar ISN'T there to begin with"
    - url: "/api/person/10/"
    - expected_status: [404]

- test:
    - group: "Quickstart"
    - name: "Basic smoketest"
    - url: "/api/person/"

- test:
    - group: "Quickstart"
    - name: "Create a person"
    - url: "/api/person/10/"
    - method: "PUT"
    - body: '{"first_name": "Gaius","id": 10,"last_name": "Baltar","login": "baltarg"}'
    - headers: {'Content-Type': 'application/json'}

- test:
    - group: "Quickstart"
    - name: "Make sure Mr Baltar is there after we added him"
    - url: "/api/person/10/"

- test:
    - group: "Quickstart"
    - name: "Get rid of Gaius Baltar!"
    - url: "/api/person/10/"
    - method: 'DELETE'

- test:
    - group: "Quickstart"
    - name: "Make sure Mr Baltar ISN'T there after we deleted him"
    - url: "/api/person/10/"
    - expected_status: [404]

上述文件已经形成了从创建-查询-删除-查询的用户验证生命周期环。此外还支持基本的认证。

---
- config:
    - testset: "Quickstart authentication test"

- test:
    - name: "Authentication using basic auth"
    - url: "/api/person/"
    - auth_username: "foobar"
    - auth_password: "secret"
    - expected_status: [200]

至此,快速入门部门撰写完毕。本部分主要目的如下:

  • 了解文件的格式,能通过简单模仿操作,培养对该工具的使用兴趣。
  • 了解文件api涉及核心,单独文件最好覆盖一个完整的业务流,形成业务闭环。
  • 涉及了预期结果的比对,expected_status,该函数是对响应状态码进行断言。

1 对 “【PyRestTest】PyRestTest入门引导”的想法;

发表评论

电子邮件地址不会被公开。