在没有编写任何代码的情况下执行pytest

pytest-play是什么

可以在不编写代码的情况下进行测试。

虽然测试代码是必需的,但制作它确实很辛苦,就像填写纳税申报一样复杂。
pytest-play是一个能够仅需编写YAML文件即可执行测试的库。
由于不需要编写代码,所以似乎可以让非程序员也能够委托他人进行测试。

功能 –

有以下的功能。

    • 実行時間の計測

 

    • JUnit XML report

 

    • StatsD/graphiteを用いたテストメトリクスの監視

 

    • プラグイン

play_selenium
play_requests
play_sql
play_cassandra
play_dynamodb
play_websocket
play_mqtt

使用play_requests或play_selenium,可以对Django、Flask等Web服务器进行测试。Web服务器可以是任何Python制作的,也可以是使用Rails等制作的。

安装

通过pip进行安装。

pip install pytest-play

无论是否在安装了Python的环境中,都可以通过Docker来执行。

docker run -i --rm -v $(pwd):/src davidemoro/pytest-play

试试看

我要进行一个简单的测试。和pytest类似,以“test”开头的文件名的YAML文件将作为测试对象。

- comment: 足し算のテスト
  provider: python
  type: assert
  expression: "1 + 1 == 2"

创建上述的test_add.yml并执行测试。可以直接使用pytest命令。

$ pytest 
======================================== test session starts =========================================
platform linux -- Python 3.6.7, pytest-4.3.0, py-1.8.0, pluggy-0.9.0
rootdir: /home/driller/work/pytest-play, inifile:
plugins: variables-1.7.1, play-2.2.0
collected 1 item                                                                                     

test_add.yml .                                                                                 [100%]

====================================== 1 passed in 0.05 seconds ======================================

简单来说,YAML的配置项如下所示。

    • provider: 実行するコマンドを指定します、pythonやseleniumなどがあります

 

    • type: providerのサブカテゴリのようなものです(うまく説明できない)

 

    • expression: 評価式を渡します

 

    comment: コメントです

变量

在中文中,以以下方式重新表达:
在type中存储变量,在name中指定变量名,在expression中指定值。
表达式必须用引号括起来。

- comment: 変数を代入
  provider: python
  type: store_variable
  name: spam
  expression: "'ham'"
- comment: 変数をテスト
  provider: python
  type: assert
  expression: "'$spam' * 2 == 'hamham'" 
$ pytest 
======================================== test session starts =========================================
platform linux -- Python 3.6.7, pytest-4.3.0, py-1.8.0, pluggy-0.9.0
rootdir: /home/driller/work/pytest-play, inifile:
plugins: variables-1.7.1, play-2.2.0
collected 2 items                                                                                    

test_add.yml .                                                                                 [ 50%]
test_var.yml .                                                                                 [100%]

====================================== 2 passed in 0.06 seconds ======================================

利用requests进行测试

在使用request进行网站测试时,需要安装play_requests。

pip install play_requests

作为例子,我们来测试Weather Hacks的API。
我们将安装play_requests并将其指定为提供者。
city=040020代表宫城县白石市的ID。我们将测试pinpointLocation的第一个目标是仙台市西部。

- comment: 白石市の天気
  provider: play_requests
  type: GET
  url: http://weather.livedoor.com/forecast/webservice/json/v1?city=040020
  assert: response.status_code == 200
  variable: json
  variable_expression: response.json()
- comment: "json"
  type: assert
  provider: python
  expression: "variables['json']['pinpointLocations'][0]['name'] == '仙台市西部'"

$ pytest 
======================================== test session starts =========================================
platform linux -- Python 3.6.7, pytest-4.3.0, py-1.8.0, pluggy-0.9.0
rootdir: /home/driller/work/pytest-play, inifile:
plugins: variables-1.7.1, play-2.2.0
collected 3 items                                                                                    

test_add.yml .                                                                                 [ 33%]
test_var.yml .                                                                                 [ 66%]
test_weather_hacks.yml .                                                                       [100%]

====================================== 3 passed in 0.20 seconds ======================================

使用Selenium进行测试

使用Selenium进行播放。
安装方法相同,但需要单独安装Webdriver。

pip install play_selenium

作为例子,我们尝试在google.com输入搜索词。

- comment: Google.comにアクセス
  provider: selenium
  type: get
  url: https://google.com
- comment: 検索ワードを入力
  type: setElementText
  provider: selenium
  locator:
    type: name
    value: q
  text: "cp932撲滅"
- type: sendKeysToElement
  provider: selenium
  locator:
    type: name
    value: q
  text: ENTER
- comment: 画面をじっくり眺める時間
  type: pause
  provider: selenium
  waitTime: 10000
$ pytest 
======================================== test session starts =========================================
platform linux -- Python 3.6.7, pytest-4.3.0, py-1.8.0, pluggy-0.9.0
rootdir: /home/driller/work/pytest-play, inifile:
plugins: variables-1.7.1, splinter-2.0.1, pypom-navigation-2.0.3, play-2.2.0
collected 4 items                                                                                    

test_add.yml .                                                                                 [ 25%]
test_google.yml .                                                                              [ 50%]
test_var.yml .                                                                                 [ 75%]
test_weather_hacks.yml .                                                                       [100%]

===================================== 4 passed in 14.53 seconds ======================================

运行后,浏览器将启动并显示搜索结果页面。

杂感

大致试用后的感想是,相较于省去编写代码的麻烦,我认为将其转化为YAML可以使外观更加简洁,这是它的优点。
虽然没有介绍完所有功能,但若有机会,我希望能介绍。

广告
将在 10 秒后关闭
bannerAds