Scalatest 是否支持假设? [英] Does Scalatest have any support for assumptions?

查看:43
本文介绍了Scalatest 是否支持假设?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据标题,我想知道在定义特定测试用例时是否可以向 Scalatest 提供假设".在这种情况下,假设是测试的先决条件,如果假设评估为假,测试将被跳过而不是执行(并由运行者相应地处理).

As per the title, I'm wondering if it's possible to provide "assumptions" to Scalatest when defining a particular test case. Assumptions in this context would be preconditions for a test, such that if the assumption evaluated to false, the test would be skipped rather than executed (and handled accordingly by the runners).

在这种特殊情况下,我正在考虑测试之间的依赖关系 - 所以可能会有一个基本测试来验证一个方法是否返回任何东西,然后是深入细节的后续测试的响应.如果前一个测试失败,我宁愿将后一个测试以某种方式标记为不可运行",而不是让它们也失败.

In this particular case, I'm thinking about dependencies between tests - so there might be a basic test that validates whether a method returns anything at all, followed by later tests that drill into the specifics of the response. If the former test fails, I'd rather have the latter test marked as "not runnable" in some way, rather than have them fail as well.

也就是说,我可以想象将来使用它来定义一些未连接的先决条件(例如硬盘驱动器必须有至少 20MB 的可用空间),所以如果有在运行时跳过测试的一般方法(而不是使用ignorepending) 我更愿意听到.

That said I can imagine using this in future to define some unconnected preconditions (such as the hard drive must have at least 20MB of space free), so if there's a general way of skippin a test at runtime (as opposed to using ignore or pending) I'd prefer to hear that.

欢迎使用特殊语法,但如果我必须手动抛出某种异常也没关系.

Specialised syntax is welcome, though if I have to manually throw a certain kind of exception that's OK too.

推荐答案

ScalaTest 2.0(我认为)添加了对假设的支持:

ScalaTest 2.0 (I think) added support for assumptions:

Trait Assertions 还提供了允许您取消测试的方法.如果测试所需的资源不可用,您将取消测试.例如,如果测试需要外部数据库联机,而它不是,则可以取消测试以表明由于缺少数据库而无法运行.这样的测试假设有一个数据库可用,你可以在测试开始时使用假设方法来表明这一点,就像这样:

Trait Assertions also provides methods that allow you to cancel a test. You would cancel a test if a resource required by the test was unavailable. For example, if a test requires an external database to be online, and it isn't, the test could be canceled to indicate it was unable to run because of the missing database. Such a test assumes a database is available, and you can use the assume method to indicate this at the beginning of the test, like this:

assume(database.isAvailable)

http://www.scalatest.org/user_guide/using_assertions

这篇关于Scalatest 是否支持假设?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆