Visual Studio团队测试:如何进行单元测试";只使用Asserts()而不使用任何工具的运算符

本文关键字:Asserts 运算符 工具 任何 测试 团队 Studio 何进行 quot 单元测试 Visual | 更新日期: 2023-09-27 18:30:03

我需要编写一些单元测试用例来测试C#Visual Studio Team test框架中的代码。以下是我想要测试的方法:

public static ObjectID CreateObjectID(ObjectID xrmObjectID)
{
        return new ObjectID
        {
            Id = xrmAssociation.ID != null ? xrmAssociation.ID.Id : Guid.Empty;
        };
}

在上面的方法中,我需要编写单元测试用例来覆盖条件语句,例如:

Id = xrmAssociation.ID != null ? xrmAssociation.ID.Id : Guid.Empty;

所以我写了以下单元测试方法:

namespace WebApi.Test
{
    [TestClass]
    public class ApiTest
    {
        [TestMethod]
        [ExpectedException(typeof(NullReferenceException), "A userId of null was inappropriately allowed.")]
        public void CreateObjectIDShouldCheckConditionalBranch()
        {
            Xrm.objectID Input = new Xrm.objectID();
            Input = null;
            WebApiRole.Api.CreateObjectID(Input);
            var expected = default(WebApi.ObjectID);
            Assert.IsTrue(expected == WebApi.CreateObjectID(Input), "Failed");
        }
    }
}

这个测试通过了,但它实际上并没有测试我想要测试的东西,即当通过"null"时,它应该分配"Guid.Empty"。它只是抛出NullReference的异常,从而使测试通过。

Visual Studio团队测试:如何进行单元测试";只使用Asserts()而不使用任何工具的运算符

我建议为每个单独的案例编写一个测试。这样,如果需求发生变化,您可以更容易地调整测试代码。

我会这样做(我也会猜测并假设你正在建模Dynamics CRM,根据数据判断)

[TestMethod]
public void AssociationCreationFromXrmShouldDefaultWhenAssociationHoldingIsNull()
{
    Xrm.pv_association input = new Xrmpv_association();
    input.pv_AssociationHolding = null;
    var output = PVWebApiRole.ApiModelFactory.CreateAssociationFromXrm(Input);
    // The fact that 'output' is valid should be tested separately
    Assert.AreEqual(output.AssociationHoldingId, Guid.Empty);
}
[TestMethod]
public void AssociationCreationFromXrmShouldKeepNotNullAssociationHolding()
{
    var sampleReference = new EntityReference("yourlogicalName", Guid.Parse("00000000-0000-0000-0000-000000000000"));
    Xrm.pv_association input = new Xrmpv_association();
    input.pv_AssociationHolding = sampleReference;
    var output = PVWebApiRole.ApiModelFactory.CreateAssociationFromXrm(Input);
    // The fact that 'output' is valid should be tested separately
    Assert.AreEqual(output.AssociationHoldingId, sampleReference.Id);
}            

等等,每个字段有两个测试,一个测试条件的true侧,一个用于false侧(可以构建和调用两个通用方法,一个针对OptionSet字段,一个面向EntityReference字段,使代码编写起来又短又快)。

此外,如果inputnull,我认为您应该调整CreateAssociationFromXrm,使其抛出ArgumentException(当然要提前编写一些测试)。

使用ExpectedException注释可能有风险,尤其是在检查可能在其他情况下引发的NullReferenceException之类的一般异常时。

看起来你想做的是检查一个特定的属性是否为null,在这种情况下,你最好使用IsNotNull()断言,例如

var actual = PVWebApiRole.ApiModelFactory.CreateAssociationFromXrm(Input);
Assert.IsNotNull(actual.CreatedByUserProfileId);

有关调试NullReferenceException的一般帮助,请参阅什么是NullReferenceException以及如何修复它?

正如我在评论中已经指出的,您的问题是CreateAssociationFromXrm()方法在进行以下检查时假设xrmAssociation参数为非null:

xrmAssociation.pv_CreatedByUserProfileID != null ? xrmAssociation.pv_CreatedByUserProfileID.Id : Guid.Empty

您可以通过在特定的条件语句中添加空检查来避免这种情况,例如

xrmAssociation != null && xrmAssociation.pv_CreatedByUserProfileID != null ? xrmAssociation.pv_CreatedByUserProfileID.Id : Guid.Empty

风格建议

通常,最好完全禁止null输入,因此您可能实际上想在方法开始时检查null,并抛出ArgumentNullException,例如

public static Association CreateAssociationFromXrm(Xrm.pv_association xrmAssociation)
{
  if (xrmAssociation == null) throw new ArgumentNullException("xrmAssociation", "xrmAssociation cannot be null");
  // Rest of your existing logic goes here
}