2015-07-21 69 views
1

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

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; 
} 

与其他一些类结果的是依赖于这两个枚举。

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个枚举的所有组合。 喜欢:

对于RAM: 测试用例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,_test RES = 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

对我来说,这些不是测试用例,而是结果组合。 –

+0

当我假设你不想要所有的排列,但只有某些排列时,我是对的吗?就像你不希望任何组合的测试结果不同'组'(testID = 1 testRES = OK和testID = 4 testRES = Fail会是一个无效组合)?我认为这很容易实现。只要让我知道,如果这是你需要的。 –

回答

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; 
    } 
}; 

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

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

+0

嗨威廉, 感谢您的及时回复。我试图编辑我的问题,以便它会更清晰。 感谢您解决我的一个问题,那就是获得2枚枚举的所有排列组合。我使用'Tuple'而不是'Pair',因为我无法在c#4.0中找到'Pair'类。 但我想要做的是创建不同类型的测试用例,如我的问题所示。但我卡住了。 – Kai