michaelhall/webunit

Webunit 测试客户端

v2.2.0 2023-02-14 19:15 UTC

This package is auto-updated.

Last update: 2024-09-14 22:28:20 UTC


README

Tests StyleCI License Latest Stable Version Total Downloads

Webunit 是一个用于自动化网络应用程序测试的命令行客户端。

需求

  • PHP >= 8.0

使用 Composer 安装

$ composer require michaelhall/webunit

基本用法

webunit 客户端需要一个包含要运行的测试的文本格式文件。将此文件的名称作为命令行参数传递

$ webunit testfile

测试文件中的测试由一个或多个测试用例组成。每个测试用例都以一个命令开始。最简单的测试用例只是一个命令,例如

get https://example.org/

如果 URL https://example.org/ 功能正常且不返回错误或重定向状态码,则此测试将成功。否则,测试将失败。

还可以使用其他支持的 HTTP 方法获取 URL

delete https://example.org/
patch https://example.org/
post https://example.org/
put https://example.org/

测试用例还可以包含特定的断言

get https://example.org/
assert-contains Example

可以使用注释和空格来格式化测试文件

# This is a comment.
get                    https://example.org/
assert-contains        Example

# Another test case.
get                    https://example.org/foobar
assert-status-code     404

某些断言可以通过修饰符字符进行修改

get                    https://example.org/

# The "!" modifier negates the assertion.
# This assertion will pass if the returned status code is not 404
assert-status-code!    404

# The "^" modifier makes the assertion case insensitive.
# This assertion will pass if result contains "Example", "EXAMPLE", "eXaMpLe" etc.
assert-contains^       example

# The "~" modifier evaluates the assertion as a regular expression.
# This assertion will pass if result contains "Example" or "example".
assert-contains~       [Ee]xample

# Modifiers can be combined.
# This assertion will fail if result contains "FooBar", "foobar" etc.
assert-contains!^      foobar

可以使用请求修饰符修改请求

put                    https://example.org/
with-header            Content-Type: application/json
with-raw-content       {"Foo": "Bar"}

测试文件可以设置变量以供测试重用。

变量在解析时以类似于 C 和 C# 等语言中的预处理器指令的方式评估。

# Set the variable "Url" to the value "https://example.com".
set                    Url = https://example.com/

# get https://example.com/
get                    {{ Url }}

# get https://example.com/another-page
get                    {{ Url }}another-page

还可以从命令行设置变量

$ webunit --set=Url=https://example.com/ testfile
# get https://example.com/
get                    {{ Url }}

如果变量尚未设置,则可以使用默认值设置变量。

示例 1

$ webunit testfile
# "Url" is not set. Set the value to "https://example.com/".
set-default            Url = https://example.com/

# get https://example.com/
get                    {{ Url }}

示例 2

$ webunit --set=Url=https://example.org/ testfile
# "Url" is already set to "https://example.org/". Do not change it.
set-default            Url = https://example.com/

# get https://https://example.org/
get                    {{ Url }}

在测试文件中可以使用以下转义序列

get                    https://example.org/
assert-contains        <html>\n<head>\n\s

命令

delete url

通过 DELETE 请求获取 URL。

delete https://example.org/

get url

通过 GET 请求获取 URL。

get https://example.org/

patch url

通过 PATCH 请求获取 URL。

patch https://example.org/

post url

通过 POST 请求获取 URL。

post https://example.org/

put url

通过 PUT 请求获取 URL。

put https://example.org/

断言

assert-contains content

断言结果内容包含指定的内容。允许的修饰符是 !^~

assert-contains Foo

assert-empty

断言结果内容为空。允许的修饰符是 !

assert-empty

assert-equals content

断言结果内容与指定的内容相同。允许的修饰符是 !^~

assert-equals Foo

assert-header header-name[: header-value]

断言结果包含具有指定名称和可选值的头。允许的修饰符是 !^~

assert-header Location
assert-header Location: https://example.com/

注意:头名称始终不区分大小写。

assert-status-code status-code

断言结果的状态码与指定的状态码相同。允许的修饰符是 !

注意:如果结果具有状态码 200-299 之外的值,则此断言必须存在才能使测试通过。

assert-status-code 301

请求修饰符

with-header header-name: header-value

将指定的名称设置为具有指定值的 HTTP 头。

with-header Accept-Language: en

with-post-file parameter-name = file-path

将指定的名称设置为要上传的文件。文件路径可以是绝对路径,也可以是相对于 webunit 测试脚本的相对路径。此请求修饰符不能用于 GET 请求,并且不能与 with-raw-content 请求修饰符结合使用。

with-post-file File = ../../files/foo.txt

with-post-parameter parameter-name = parameter-value

将指定名称的POST参数设置为指定的值。此请求修饰符不能用于GET请求,且不能与with-raw-content请求修饰符组合使用。

with-post-parameter Text = Foo

with-raw-content 内容

将请求体内容设置为指定的内容。此请求修饰符不能用于GET请求,且不能与with-post-filewith-post-parameter请求修饰符组合使用。

with-raw-content {"Foo": "Bar"}

许可证

MIT