如何在Pex中定义一组输入参数?
假设我的 MyClass 有 100 个字段。
如果我使用 MyClass 的对象作为输入参数,Pex 会在尝试生成所有可能的组合时感到窒息(即使在简单的测试中,我的也会遇到 1000 个路径)
[PexMethod] void MytestMethod(MyClass param){...}
我如何告诉 Pex 仅使用 MyClass 的一组预定义对象,而不是让它变得智能并生成所有可能的组合进行测试?
换句话说,我想在上面的代码中手动指定 param 的可能状态列表,并告诉 Pex 使用它
干杯
Say I have MyClass with 100s of fields.
If I use an object of MyClass as an input param, Pex would simply choke trying to generate all possible combinations (mine runs into 1000s of paths even on a simple test)
[PexMethod]
void MytestMethod(MyClass param){...}
How can I tell Pex to use only a set of predefined objects of MyClass rather than having it trying to be smart and generate all possible combinations to test?
In other words I want to manually specify a list of possible states for param in the code above and tell Pex to use it
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您发现 Pex 生成大量不相关、冗余或其他无用的输入,您可以使用
PexAssume
调整它为参数化单元测试的输入生成的值,这将确保所有生成的值输入满足您提供的一组标准。例如,如果您想确保参数来自预定义的值集合,您可以执行以下操作:
PexAssume
还有其他辅助方法用于更一般的输入修剪,例如IsNotNull
、AreNotEqual
等。很少有文档表明还有一些特定于集合的功能,尽管如果这些方法存在,我对它们并不熟悉。请查看Pex 手册了解更多信息。
If you find that Pex is generating large amounts of irrelevant, redundant, or otherwise unhelpful inputs, you can shape the values that it generates for your parametrized unit tests' input using
PexAssume
, which will ensure that all generated inputs meet a set of criteria that you provide.If you were wanting to ensure that arguments came from a predefined collection of values, for instance, you could do something like this:
PexAssume
has other helper methods as well for more general input pruning, such asIsNotNull
,AreNotEqual
, etc. What little documentation is out there suggests that there is some collection-specific functionality as well, though if those methods exist, I'm not familiar with them.Check out the Pex manual for a bit more information.
Pex 不会尝试生成所有可能的值组合。相反,它会分析您的代码并尝试覆盖每个分支。因此,如果您有
,那么它将尝试创建一个具有
Property1 == "something"
的对象。因此,将测试限制在某些预定义的对象上是相当违反“Pex 哲学”的。也就是说,您可能会发现以下信息很有趣。您可以提供 Pex 工厂类。例如,请参阅这篇博文或这个一个。
然后,
Pex 将使用通过探索代码发现的适当值来调用此工厂类(而不是默认工厂)。工厂方法允许您限制可能的参数和值。
您还可以使用
PexArguments
属性来建议值,但这不会阻止 Pex 尝试生成其他值来覆盖代码中的任何分支。它只是尝试您首先提供的那些。有关
PexArguments< 的详细信息,请参阅此处 /code> 并在 参数化测试模式。
Pex will not try to generate every possible combination of values. Instead, it analyses your code and tries to cover every branch. So if you have
then it will try to create an object that has
Property1 == "something"
. So limiting the tests to some predefined objects is rather against the 'Pex philosophy'. That said, you may find the following information interesting.You can provide a Pex factory class. See, for instance, this blog post or this one.
}
Pex will then call this factory class (instead of a default factory) using appropriate values it discovers from exploring your code. The factory method allows you to limit the possible parameters and values.
You can also use
PexArguments
attribute to suggest values, but this will not prevent Pex from trying to generate other values to cover any branches in your code. It just tries the ones you provide first.See here for more information on
PexArguments
and also search for 'seed values' in the PDF documentation on Parameterized Test Patterns.