基于2个枚举的多个permations

时间:2015-07-21 17:02:00

标签: c# c#-4.0

我需要根据2个枚举的结果创建测试用例。 例如:

public enum Test_ID
{
    // for ram test
    test1 = 1,
    test2 ,
    test3 ,
    // for video test
    test4 ,
    test5 ,
    // many more...
}

public enum Test_Result
{
    NOT_FAIL = 0,
    FAIL 
}

public struct ResultStruct
{
    public Test_ID id;
    public Test_Result res;
}

其他一些类结果依赖于这2个枚举。

public class foo
{
    public ResultStruct[] ResStructArr =  new ResultStruct[MAX_NUM_OF_TESTS];

    public void updateTestsResults()
    {
        getResult();
        for (int i = 0; i <= MAX_NUM_OF_TESTS; i++)
        {
            if(ResStructArr[i].id == 1 && ResStructArr[i].res == FAIL ||
               ResStructArr[i].id == 2 && ResStructArr[i].res == FAIL ||
               ResStructArr[i].id == 3 && ResStructArr[i].res == FAIL )
               {
                   ramtest.result = FAIL;
               }
               else
               {
                   ramtest.result = NOT_FAIL;
               }

               // Update other tests results here
        }
    }

    public void getResult()
    {
         // get Test_ID and Test_Result and put it in struct array
    }

    // Perform tests..(Ram Test, Video tests, etc)
}

但是对于我的测试用例,我必须测试2个枚举的所有组合。 像:

对于拉姆: 测试用例1:

  

_testId = 1,_testRes = NOT_FAIL
  _testId = 2,_testRes = NOT_FAIL
  _testId = 3,_testRes = NOT_FAIL

测试用例2:

  

_testId = 1,_testRes = NOT_FAIL
  _testId = 2,_testRes = FAIL
  _testId = 3,_testRes = NOT_FAIL

测试案例3:

  

_testId = 1,_testRes = NOT_FAIL
  _testId = 2,_testRes = FAIL
  _testId = 3,_ testRes = FAIL

测试案例4:

  

_testId = 1,_testRes = FAIL
  _testId = 2,_testRes = NOT_FAIL
  _testId = 3,_ testRes = FAIL
  等等...

视频: 测试用例1:

  

_testId = 4,_testRes = FAIL
  _testId = 5,_testRes = FAIL

测试用例2:

  

_testId = 4,_testRes = PASS
  _testId = 5,_testRes = FAIL

测试案例3:

  

_testId = 4,_testRes = FAIL
  _testId = 5,_testRes = PASS
  等等...

我读了here我可以获得2个枚举的所有排列。但不是我想要的。

有没有办法做到这一点,还是我必须逐个手动编写测试用例?

修改

我已经编辑了我的问题,以便更清楚我想做什么。

我正在尝试按照上面描述的那样创建测试用例。在 William Custode 的帮助下,我可以获得枚举的所有排列。但我一直坚持创建测试用例。

2 个答案:

答案 0 :(得分:1)

一个简单的嵌套循环语句将产生两组值的每个变体:

var tests = Enum.GetValues(typeof(Test_ID)).Cast<Test_ID>();

var results = Enum.GetValues(typeof(Test_Result)).Cast<Test_Result>();

var pairs = new List<Pair>();

foreach(var test in tests)
{
    foreach(var result in results)
    {
        pairs.Add(new Pair
            {
                Test = test,
                Result = result
            });
    }
}

您想要对这些信息做些什么并不清楚您的问题,所以剩下的就是我的推断和建议。

看起来您的updateRamTest方法只是检查是否有任何测试失败,然后ramTest.result设置为false。那么为什么不省略对_testId的检查,只说ramTest.result = _testRes

答案 1 :(得分:0)

我现在知道你需要什么,这是我给你的答案:

public enum Test_ID
{
  Type_MASK = 100,

  RAM    = 100,
  RAM_01 = 101,
  RAM_02 = 102,
  RAM_03 = 103,
  VIDEO    = 200,
  VIDEO_01 = 201,
  VIDEO_02 = 202,
}

public enum Test_Result
{
  nothing = 0,
  OK   = 1,
  FAIL = 99,
}

首先,不应将0用作结果值之一,因为0是int变量的默认值。您可能忘记分配值并获得无效的测试结果。因此,不要使用0.帮助我防止错误。

其次,通过使用限定名称,您可以使用枚举名称区分测试用例(请参阅Enum类的静态函数,即Enum.GetNames())。更简单的方法是使用枚举值并使用除法和模来将值分成组:

(This is C++/CLI, copied from my own code and variables renamed. You can easily convert it to C#)

public ref class CTestCase abstract
{
public:
  static Test_ID Type (Test_ID i_enTest)
  {
    return i_enTest- static_cast<Test>(Number(i_enTest));
  }

  static Test_ID Type (int i_iTest)
  {
    return static_cast<Test_ID>(i_iTest- Number(i_iTest));
  }

  static int Number (Test_ID i_enTest)
  {
    int iNumber = static_cast<int>(i_enTest) % static_cast<int>(Test_ID::Type_MASK);
    return iNumber;
  }

  static int Number (int i_iTest)
  {
    int iNumber = i_iTest% static_cast<int>(Test_ID::Type_MASK);
    return iNumber;
  }
};

这不是最终的解决方案,但我想你会得到余下的。 ; - )