如何在Pex中定义一组输入参数?

发布于 2024-12-26 08:31:39 字数 299 浏览 1 评论 0原文

假设我的 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 技术交流群。

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

发布评论

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

评论(2

灼痛 2025-01-02 08:31:39

如果您发现 Pex 生成大量不相关、冗余或其他无用的输入,您可以使用 PexAssume 调整它为参数化单元测试的输入生成的值,这将确保所有生成的值输入满足您提供的一组标准。

例如,如果您想确保参数来自预定义的值集合,您可以执行以下操作:

public void TestSomething(Object a) {
    PexAssume.IsTrue(someCollection.Contains(a));
}

PexAssume 还有其他辅助方法用于更一般的输入修剪,例如 IsNotNullAreNotEqual 等。很少有文档表明还有一些特定于集合的功能,尽管如果这些方法存在,我对它们并不熟悉。

请查看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:

public void TestSomething(Object a) {
    PexAssume.IsTrue(someCollection.Contains(a));
}

PexAssume has other helper methods as well for more general input pruning, such as IsNotNull, 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.

囚你心 2025-01-02 08:31:39

Pex 不会尝试生成所有可能的值组合。相反,它会分析您的代码并尝试覆盖每个分支。因此,如果您有

if (MyObject.Property1 == "something")
{
    ...
}

,那么它将尝试创建一个具有 Property1 == "something" 的对象。因此,将测试限制在某些预定义的对象上是相当违反“Pex 哲学”的。也就是说,您可能会发现以下信息很有趣。

您可以提供 Pex 工厂类。例如,请参阅这篇博文这个一个

[PexFactoryClass]  
public partial class EmployeeFactory  
{  
  [PexFactoryMethod(typeof(Employee))]  
  public static Employee Create(  
  int i0,  
  string s0,  
  string s1,  
  DateTime dt0,  
  DateTime dt1,  
  uint ui0,  
  Contract c0  
  )  
{  

  Employee e0 = new Employee();  
  e0.EmployeeID = i0;  
  e0.FirstName = s0;  
  e0.LastName = s1;  
  e0.BirthDate = dt0;  
  e0.StartDateContract = dt1;  
  e0.Salary = ui0;  
  e0.TypeContract = c0;  
  return e0;  
}   

然后,

Pex 将使用通过探索代码发现的适当值来调用此工厂类(而不是默认工厂)。工厂方法允许您限制可能的参数和值。

您还可以使用 PexArguments 属性来建议值,但这不会阻止 Pex 尝试生成其他值来覆盖代码中的任何分支。它只是尝试您首先提供的那些。

[PexArguments(1, "foo")] // try this first
void MyTest(int i, string s) 
{
    ...
}

有关 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

if (MyObject.Property1 == "something")
{
    ...
}

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.

[PexFactoryClass]  
public partial class EmployeeFactory  
{  
  [PexFactoryMethod(typeof(Employee))]  
  public static Employee Create(  
  int i0,  
  string s0,  
  string s1,  
  DateTime dt0,  
  DateTime dt1,  
  uint ui0,  
  Contract c0  
  )  
{  

  Employee e0 = new Employee();  
  e0.EmployeeID = i0;  
  e0.FirstName = s0;  
  e0.LastName = s1;  
  e0.BirthDate = dt0;  
  e0.StartDateContract = dt1;  
  e0.Salary = ui0;  
  e0.TypeContract = c0;  
  return e0;  
}   

}

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.

[PexArguments(1, "foo")] // try this first
void MyTest(int i, string s) 
{
    ...
}

See here for more information on PexArguments and also search for 'seed values' in the PDF documentation on Parameterized Test Patterns.

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