我们将使用理论属性做什么?

发布于 2024-09-02 14:35:21 字数 149 浏览 4 评论 0原文

我在 NUnit 中发现了 [Theory][Datapoint] 属性。我不太确定应该如何使用这些。我认为它们可以用于数据驱动的测试,这引起了我的兴趣。没有太多可用的资源。有人可以向我解释如何使用它们或向我指出资源吗?谢谢。

I discovered [Theory] and [Datapoint] attributes in NUnit. I am not very sure about how should I use these. I think they can be used for data-driven testing and this has got me interested. There aren't many resources available on the same. Can someone explain to me how to use them or point me to resources? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以歌曲疗慰 2024-09-09 14:35:21

您看过在线 NUnit 文档吗?这里有一个示例向您展示如何使用 [Theory] ​​和 [Datapoint] 属性:

http://www.nunit.org/index.php?p=theory&r=2.5.3

Have you looked at the online NUnit docs? There's an example here that shows you how you can use the [Theory] and [Datapoint] attributes:

http://www.nunit.org/index.php?p=theory&r=2.5.3

凉墨 2024-09-09 14:35:21

理论是一种特殊类型的测试,用于验证一般陈述
关于正在开发的系统。正常测试是基于示例的。
也就是说,开发人员提供一个或多个输入示例并
测试代码内的预期输出或 - 在这种情况下
参数化测试 - 作为测试方法的参数。一个理论,关于
另一方面,作出一般性声明,表明其所有主张
对于满足某些假设的所有参数都将通过。

理论在 NUnit 中作为 TestFixture 中的方法实现,
它们用 TheoryAttribute ([Theory]) 进行注释。理论
方法必须始终有参数,因此看起来非常相似
乍一看参数化测试。然而,有一个理论包含
其参数的附加数据源并允许特殊
处理有关该数据的假设。关键的区别是,
不过,理论只是做出一般性陈述,并且不仅仅是
只是一组示例。

理论数据

理论的主要数据来源是一个或多个数据点
属性。 NUnit 将使用所需类型的任何字段,这些字段是
用这些属性之一进行注释,为每个属性提供数据
理论的参数。 NUnit 组装各个值
组合论证为理论提供测试用例。

除了 Datapoint 和 Datapoints 属性之外,还可以
使用任何被认可的方法来提供数据
正常参数化测试。我们建议不要使用此功能
过度使用,因为它与测试之间的区别背道而驰
基于示例和理论。然而,它可能有用,以便
保证包含特定的测试用例。

假设

理论本身负责确保提供的所有数据
满足其假设。它通过使用 Assume.That(...) 来完成此操作
构造,其工作方式与 Assert.That(...) 类似,但不会导致
失败。如果特定测试不满足假设
情况,该情况返回不确定的结果,而不是成功
或失败。

在一组测试用例上执行理论的总体结果是
确定如下:

如果所有测试用例都违反了假设,则理论
本身被标记为失败。如果任何断言失败,该理论
本身失败了。如果至少某些情况通过了规定的假设,
并且没有断言失败或异常,那么该理论
通过。

示例:

在下面的例子中,理论SquareRootDefinition验证了
平方根的实现满足以下条件
定义:

“给定一个非负数,该数的平方根始终为
非负数,当与其自身相乘时,给出原始值
数量。”

公共类 SqrtTests
{
    [数据点]
    公共双零=0;

    [数据点]
    公开双重阳性 = 1;

    [数据点]
    公共双重否定=-1;

    [数据点]
    公共双最大值 = double.MaxValue;

    [数据点]
    公共双无穷大= double.PositiveInfinity;

    [理论]
    公共无效SquareRootDefinition(双数字)
    {
        Assume.That(num >= 0.0 && num < double.MaxValue);

        双 sqrt = Math.Sqrt(num);

        Assert.That(sqrt >= 0.0);
        Assert.That(sqrt * sqrt, Is.EqualTo(num).Within(0.000001));
    }
}

摘自

A Theory is a special type of test, used to verify a general statement
about the system under development. Normal tests are example-based.
That is, the developer supplies one or more examples of inputs and
expected outputs either within the code of the test or - in the case
of Parametrized Tests - as arguments to the test method. A theory, on
the other hand, makes a general statement that all of its assertions
will pass for all arguments satisfying certain assumptions.

Theories are implemented in NUnit as methods within a TestFixture,
which are annotated with the TheoryAttribute ([Theory]). Theory
methods must always have arguments and therefore appears quite similar
to Parameterized Tests at first glance. However, a Theory incorporates
additional data sources for its arguments and allows special
processing for assumptions about that data. The key difference,
though, is that theories make general statements and are more than
just a set of examples.

Data for Theories

The primary source of data for a Theory is the Datapoint or Datapoints
attribute. NUnit will use any fields of the required types, which are
annotated with one of these attributes, to provide data for each
parameter of the Theory. NUnit assembles the values for individual
arguments combinatorially to provide test cases for the theory.

In addition to the Datapoint and Datapoints attributes, it is possible
to use any of the approaches for supplying data that are recognized on
normal parameterized tests. We suggest that this capability not be
overused, since it runs counter to the distinction between a test
based on examples and a theory. However, it may be useful in order to
guarantee that a specific test case is included.

Assumptions

The theory itself is responsible for ensuring that all data supplied
meets its assumptions. It does this by use of the Assume.That(...)
construct, which works just like Assert.That(...) but does not cause
a failure. If the assumption is not satisfied for a particular test
case, that case returns an Inconclusive result, rather than a Success
or Failure.

The overall result of executing a Theory over a set of test cases is
determined as follows:

If the assumptions are violated for all test cases, then the Theory
itself is marked as a failure. If any Assertion fails, the Theory
itself fails. If at least some cases pass the stated assumptions,
and there are no assertion failures or exceptions, then the Theory
passes.

Example:

In the following example, the theory SquareRootDefinition verifies
that the implementation of square root satisies the following
definition:

"Given a non-negative number, the square root of that number is always
non-negative and, when multiplied by itself, gives the original
number."

public class SqrtTests
{
    [Datapoint]
    public double zero = 0;

    [Datapoint]
    public double positive = 1;

    [Datapoint]
    public double negative = -1;

    [Datapoint]
    public double max = double.MaxValue;

    [Datapoint]
    public double infinity = double.PositiveInfinity;

    [Theory]
    public void SquareRootDefinition(double num)
    {
        Assume.That(num >= 0.0 && num < double.MaxValue);

        double sqrt = Math.Sqrt(num);

        Assert.That(sqrt >= 0.0);
        Assert.That(sqrt * sqrt, Is.EqualTo(num).Within(0.000001));
    }
}

Taken from

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文