PexAssert.Throws 与 Run Pex 结合使用 PUT
当我创建一个 PUT 来调用被测类的方法并使用 PexMethod
为其赋予属性时,“运行 Pex”将使用此方法并为其创建自动生成的测试。
仅当对该方法的调用直接在 PUT 主体中时才有效。
示例:
此 PUT 由“Run Pex”使用:
[PexGenericArguments(typeof(string))]
[PexGenericArguments(typeof(int))]
[PexGenericArguments(typeof(object))]
[PexMethod]
public string Convert01_ConverterForTypeNotRegistered<T>(
[PexAssumeUnderTest] ToStringConverter target, T objectToConvert)
{
var result = "";
result = target.Convert(objectToConvert);
return result;
}
此 PUT 不由“Run Pex”使用:
[PexGenericArguments(typeof(string))]
[PexGenericArguments(typeof(int))]
[PexGenericArguments(typeof(object))]
[PexMethod]
public string Convert01_ConverterForTypeNotRegistered_ThrowsInvalidOperationException<T>(
[PexAssumeUnderTest] ToStringConverter target, T objectToConvert)
{
var result = "";
PexAssert.Throws<InvalidOperationException>(() => target.Convert(objectToConvert));
return result;
}
我想表明对该方法的调用总是抛出此异常,与参数无关。
如何实现这一目标?
我已经问过问题一周前在微软官方Pex论坛但是没有收到单一答案,因此我在这里发了两篇文章。
该问题可以如下重现:
- 通过一个项目、一个类和一种方法来实现一个简单的解决方案。
- 右键单击该方法并选择“运行 Pex”。
- 在 Pex 探索结果中右键单击任何条目并选择“保存测试...”。它将创建一个包含两个相关文件的新测试项目:
和Test.cs Test. .g.cs.第一个文件是PUT,第二个文件是具体的测试方法,每个参数Pex选择一个。每次为第一个文件中包含 PUT 的方法运行 pex 时,都会自动重新创建第二个文件。 - 右键点击测试项目,选择“Pex”-> “删除生成的单元测试”。这将从第二个文件中删除特定测试。
- 转到包含 PUT 的文件并将 PUT 重命名为任意名称。
- 返回到第 1 点的方法,再次右键单击并选择“运行 Pex”。
- Pex 将在测试项目中创建一个名为“Test..g.cs”的新文件。尽管您对其进行了重命名,但它使用与以前相同的 PUT。
- 再次更改 PUT,保留名称不变,但将内容更改为我使用 PexAssert.Throws 的第二个示例。
- 再次右键单击测试项目并选择“Pex”-> “删除生成的单元测试”。
- 再次右键单击第 1 点中的方法,然后选择“运行 Pex”。
- 打开测试项目中的两个 *.g.cs 文件,您将看到没有测试。这意味着,Pex 没有使用 PUT。这就是问题所在:)
when I create a PUT that calls a method of the class under test and attribute it with PexMethod
, "Run Pex" will use this method and create automatic generated tests for it.
This only works as long as the call to the method is directly in the body of the PUT.
Sample:
This PUT is used by "Run Pex":
[PexGenericArguments(typeof(string))]
[PexGenericArguments(typeof(int))]
[PexGenericArguments(typeof(object))]
[PexMethod]
public string Convert01_ConverterForTypeNotRegistered<T>(
[PexAssumeUnderTest] ToStringConverter target, T objectToConvert)
{
var result = "";
result = target.Convert(objectToConvert);
return result;
}
This PUT is not used by "Run Pex":
[PexGenericArguments(typeof(string))]
[PexGenericArguments(typeof(int))]
[PexGenericArguments(typeof(object))]
[PexMethod]
public string Convert01_ConverterForTypeNotRegistered_ThrowsInvalidOperationException<T>(
[PexAssumeUnderTest] ToStringConverter target, T objectToConvert)
{
var result = "";
PexAssert.Throws<InvalidOperationException>(() => target.Convert(objectToConvert));
return result;
}
I want to show that a call to the method always throws this exception, independent of the parameters.
How to achieve this?
I already asked that question a week ago in the official Pex forum of Microsoft but didn't receive a single answer, therefore I am doing a double post here.
The problem can be reproduced like the following:
- Have a simple solution with one project with one class with one method.
- Right click on that method and choose Run Pex.
- In the Pex Exploration Results right click on any entry and choose "Save Test...". It will create a new test project with two relevant files:
<class-name>Test.cs
and<class-name>Test.<MethodName>.g.cs
. In the first file, there is the PUT, in the second files are the specific test methods, one for each parameter Pex chooses. The second file is automatically recreated every time you run pex for a method that has a PUT in the first file. - Right click on the test project and choose "Pex" -> "Delete generated unit tests". This will remove the specific tests from the second file.
- Go to the file with the PUT and rename the PUT to an arbitrary name.
- Go back to the method from point 1 and again right-click and choose Run Pex.
- Pex will create a new file in the test project named `Test..g.cs. It uses the same PUT as before, although you renamed it.
- Change the PUT again by leaving the name as it is but changing the content to my second example with PexAssert.Throws.
- Again, right click on the test project and choose "Pex" -> "Delete generated unit tests".
- Again, right click in the method from point 1 and choose "Run Pex"
- Open both of the *.g.cs files in the test project and you will see, that there are no tests. This means, Pex didn't use the PUT. That's the problem :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚尝试在 Visual Studio 2010 中运行您的两个示例。我更改了第二个示例以使用名为
ThrowingConverter
的类,这些是我愚蠢的实现:我运行了 Pex,它提出了以下测试用例:
ToStringConverter
:整数
0
1
字符串
null
(失败并出现NullReferenceException
)""
对象
null
(失败并出现NullReferenceException
)新对象()
ThrowingConverter
:整数
0
字符串
空
""
(失败,因为它返回""
)对象
空
""
(失败,因为它返回""
)这正是我期望它找到的,所以我不'真的没有看到你的问题。
I just tried running both of your examples, in Visual Studio 2010. I changed the second example to use a class called
ThrowingConverter
, and these are my silly implementations:I ran Pex, and it came up with the following test cases:
ToStringConverter
:int
0
1
string
null
(fails withNullReferenceException
)""
object
null
(fails withNullReferenceException
)new object()
ThrowingConverter
:int
0
string
null
""
(fails because it returns""
)object
null
""
(fails because it returns""
)This is exactly what I expected it to find, so I don't see your problem really.