Microsoft文档 for IntelliTest说:
IntelliTest研究了生成测试数据和一组单元测试的.NET代码。对于代码中的每个语句,都会生成一个将执行该语句的测试输入。对代码中的每个条件分支执行一个案例分析。例如,分析
if语句、断言和所有可能引发异常的操作。此分析用于为每个方法的参数化单元测试生成测试数据,从而创建代码覆盖率较高的单元测试。
我正在使用2017。在我的方法中右击,选择"IntelliTest“,然后选择"Create IntelliTest”。我接受了弹出对话框中的所有默认设置,并点击"OK“。
public static int ScanInternalInbox()
{
if (<code removed>)
{
<code removed>;
}
<code removed>;
try
{
<code removed>;
}
catch (Exception e)
{
<code removed>;
return 0;
}
<code removed>;
try
{
<code removed>;
}
catch (Exception e)
{
<code removed>;
}
<code removed>;
foreach (<code removed>)
{
<code removed>;
if (<code removed>)
{
if (<code removed>)
{
<code removed>;
if (<code removed>)
{
foreach (<code removed>)
{
<code removed>;
if (<code removed>)
{
if (<code removed>)
{
<code removed>;
}
}
}
if (<code removed>)
{
if (<code removed>)
{
<code removed>;
}
else
{
<code removed>;
}
}
}
else
{
<code removed>;
}
}
else
{
<code removed>;
}
}
else
{
<code removed>;
}
}
<code removed>;
}那么,为什么我的方法和很多(太多)的if只生成这个单元测试代码呢?
public partial class HMR_AgentTest {
/// <summary>Test stub for ScanInternalInbox()</summary>
[PexMethod]
public int ScanInternalInboxTest() {
int result = global::HMR_Agent.HMR_Agent.ScanInternalInbox();
return result;
// TODO: add assertions to method HMR_AgentTest.ScanInternalInboxTest()
}
}我希望每个if至少有一个测试,以完全覆盖代码(几乎)。我做错什么了吗?是否有一种方法可以像微软宣称的那样生成默认测试?
编辑
我现在也运行了IntelliTest,它生成了以下代码:
public partial class HMR_AgentTest {
[TestMethod]
[PexGeneratedBy(typeof(HMR_AgentTest))]
[PexRaisedException(typeof(TypeInitializationException))]
public void ScanInternalInboxTestThrowsTypeInitializationException908()
{
int i;
i = this.ScanInternalInboxTest();
Assert.AreEqual<int>(-1, i);
}
}测试失败,因为会抛出异常,即使测试预期会抛出异常。
发布于 2017-11-27 23:57:10
[PexMethod]有效地标识了您想要测试的函数。您需要在IntelliTest上第一次右键单击并“-> Run IntelliTest”来生成包含所有执行路径测试的子.g.cs文件。
只有在更改更改执行路径或更改行为的代码时,才再次“运行IntelliTest”;如果只是重构代码,则不需要生成新的测试代码,只需在g.cs文件中正常运行生成的[TestMethod]测试即可。
https://stackoverflow.com/questions/47521614
复制相似问题