简体   繁体   English

使用不同数据类型的相同参数调用测试方法两次

[英]Call a test method twice with same parameter of different datatype

I'm trying to reduce the duplicate code by combining two tests validation methods.我试图通过结合两种测试验证方法来减少重复代码。 Both test methods pass three parameters (actualResponse, expectedReponse, filterParams) but the problem is that even if the two methods have define the same name for parameters their datatype are different.两种测试方法都传递三个参数(actualResponse、expectedReponse、filterParams),但问题是即使这两种方法为参数定义了相同的名称,它们的数据类型也不同。

Here is the summary of the script:这是脚本的摘要:

TestSteps.cs class where these methods are called: TestSteps.cs class 其中调用了这些方法:

public class TestSteps : BaseTest
{

     // _result holds the actual results coming from context get cal
     private Context _result = new();

     // _SolutionExpectedResponse have expected response from json file.
     private readonly Context _SolutionExpectedResponse = new();

    public TestSteps()
      {
        string jsonstring = File.ReadAllText(@Path of response.json file);
        _SolutionExpectedResponse = JsonConvert.DeserializeObject<Context>(jsonstring);
      }
      [When(@Call context api)]
      public void WhenCallContextAPI()
      {
        _result = Context.GetAsync(token,resource).Result;
      }

      [Then(@Verify the response Values)]
      public void ThenVerifyTheResponseValues()
      {
        ValidateDataValues(_result.Data, _SolutionExpectedResponse.Data, new string[] {"data"})

        ValidateSensitiveDataValues(_result.SensitiveData, _SolutionExpectedResponse.SensitiveData, new string[] {"sensitiveData"})
      }
}

BaseTest.cs class where validate methods were wrote: BaseTest.cs class 其中写了验证方法:

Method #1:方法#1:

Protected void ValidateDataValues(List<DataReadable> actualResponse,  List <DataReadable> expectedResponse,  string[] filterParams)

{

  if (filterParams.contains("data"))
  {
    if(actualResponse !=null)
    {
        for(int i=0; i < expectedResponse.Count; i++)
        {
           if((actualResponse[i].Key !=null)
                      {
                actualResponse[i].Key.Value.ToString().Should.BeEquivalentTo(expectedResponse[i].Key.Value.ToString());
              }
        }
        return;
    }
    else
    {
       actualResponse.Should().BeNull();
    }
  }

}

Method #2:方法#2:

Protected void ValidateSensitiveDataValues(List<SensitiveDataReadable> actualResponse,  List <SensitiveDataReadable> expectedResponse,  string[] filterParams)

{

  if (filterParams.contains("sensitiveData"))
  {
    if(actualResponse !=null)
    {
        for(int i=0; i < expectedResponse.Count; i++)
        {
           if((actualResponse[i].Key !=null)
                      {
                actualResponse[i].Key.Value.ToString().Should.BeEquivalentTo(expectedResponse[i].Key.Value.ToString());
              }
        }
        return;
    }
    else
    {
       actualResponse.Should().BeNull();
    }
  }

}

Is it possible for me to reduce the duplicate code by adding method#2 parameters in method#1 and eliminating method #2?我是否可以通过在方法#1 中添加方法#2 参数并消除方法#2 来减少重复代码?

OR或者

By calling method #1 from the method #2 by converting the datatype?通过转换数据类型从方法#2 调用方法#1?

A typical approach would be to use generics, this lets you define a method that can take any type, or some subset of types as parameters:一种典型的方法是使用 generics,这使您可以定义一个可以采用任何类型或某些类型子集作为参数的方法:

public bool ValidateSensitiveDataValues<T>(IEnumerable<T> actual, IEnumerable<T> expected, IEqualityComparer<T> comparer){
    return actual.SequenceEqual(expected, comparer);
}

Note that if T is a class you either need to give the method a EqualityComparer-object, or restrict the method to only work if T implements IEquatable: ValidateSensitiveDataValues<T>(IEnumerable<T> actual, IEnumerable<T> expected) where T: IEquatable<T> .请注意,如果 T 是 class,您需要为该方法提供一个 EqualityComparer 对象,或者限制该方法仅在T实现 IEquatable 时才有效: ValidateSensitiveDataValues<T>(IEnumerable<T> actual, IEnumerable<T> expected) where T: IEquatable<T> I tend to prefer the former since it is more flexible.我倾向于更喜欢前者,因为它更灵活。 If you do not do this any reference objects will use reference equality, and that is probably not what you want.如果您不这样做,任何引用对象都将使用引用相等性,而这可能不是您想要的。

You might also want to use a unit test library.您可能还想使用单元测试库。 NUnit is a poppular one that has Assert-methods like CollectionAssert.AreEqual and CollectionAssert.AreEquivalent . NUnit 是一种流行的方法,它具有CollectionAssert.AreEqualCollectionAssert.AreEquivalent等断言方法。 Where only the former care about ordering of the items.只有前者关心项目的排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM