Revel 测试


当前第2页 返回上一页

例如,开发人员从浏览器中访问 /@tests:

然后,增加一个测试方法:

func (t AppTest) TestSomethingImportant() {
    t.Get("/")
    t.AssertOk()
    t.AssertContentType("text/xml")
}

然后,刷新浏览器,看看新的测试:

运行测试:

嗯哼,,,行不通哦,,,修改代码使用“text/html” 替换 “text/xml”类型。

    t.AssertContentType("text/html")

然后,重新运行测试:

成功啦!

运行非交互式测试

Revel 命令行工具 提供了一个 test 命令,允许在命令行中运行测试。

下面是一个示例会话:

$ revel test github.com/revel/revel/samples/booking dev
~
~ revel! http://revel.github.com/revel
~
INFO  2012/11/09 19:21:02 revel.go:237: Loaded module testrunner
Open DB
Listening on port 9000...
INFO  2012/11/09 19:21:06 test.go:95: Testing Booking example (github.com/revel/revel/samples/booking) in dev mode
Go to /@tests to run the tests.

1 test suite to run.

AppTest                 PASSED        0s

All Tests Passed.

您还可以运行单个测试套件,或套件内的方法,用句点分隔参数:

$ revel test github.com/revel/revel/samples/booking dev ApplicationTest
$ revel test github.com/revel/revel/samples/booking dev ApplicationTest.TestThatIndexPageWorks

在控制台测试套件只有一个简单的合格/不合格显示。更详细的结果写入到文件系统:

$ cd src/github.com/revel/revel/samples/booking
$ find test-results
test-results
test-results/app.log
test-results/AppTest.passed.html
test-results/result.passed

它写三点不同:

  1. 应用程序的标准输出和标准错误重定向到 app.log
  2. 每个测试套件有一个HTML文件被写入,说明测试通过或失败。
  3. 无论 result.passed 或 result.failed 被写入, 这取决于整体的成功。

对于整合持续构建测试,有两点建议:

  1. 检查返回码,0代表测试成功,否则为非0值。
  2. 测试运行后要求存在 result.success, 或禁止 result.failed存在。

注意事项

Revel 做了什么:

  • 扫描嵌入TestSuite类型 (transitively) 的源代码
  • 在生成的 main.go 文件中,为 revel.TestSuites 类型的变量设置一个列表
  • 按要求,使用反射来查找所有以“Test”开头的TestSuite类型的方法,并调用它们来运行测试。
  • 从错误或失败的断言捕获恐慌,显示错误。

当 testrunner 模块激活后,测试代码才会被构建。

开发计划

改进测试框架:

  • 固定存储测试数据。
  • 日志写入到一个文件中(而不是 stderr / stdout)也应该被重定向到 test-results/app.log

标签:Revel

返回前面的内容

相关阅读 >>

Revel 概念

Revel 消息处理

Revel 常见问题

Revel 模板

Revel 部署

Revel 介绍

Revel 配置文件app.conf

Revel websockets

Revel 参数绑定

Revel 缓存cache

更多相关阅读请进入《Revel》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...