user940178
user940178

Reputation: 53

How to generate test data according to logic expression?

As the subject, I'd like to generate test data to cover all the possible conditions for some logic expression like below:

 ((a==3000710)||(b==3000700))
   &&(b>=30 && b<=33)
   &&((c==1)||(c>=4 && c<=6))
   &&((v==1.0.9)||(v==2.0.0))

Any comment is welcome.

BTW, the logic expression is a simplified rule which is applied on our back-end server.

Upvotes: 0

Views: 188

Answers (1)

buxter
buxter

Reputation: 593

The first thing that I must say - refactor it! Break it down to several if statements that would be easier to validate, reverse logic and exit early. It's difficult to give more detailed suggestions without seeing the actual code and the context.

Another thing, if (b == 3000700), then &&(b>=30 && b<=33) returns false, which makes this part of the statement ||(b==3000700) pointless. Maybe it was supposed to be (a == 3000700)?

Regarding the test cases... Yet again without seeing the full code snippet and knowing the context it is a bit difficult to provide meaningful suggestions. But I will try anyway.

Let's look at the "critical values" for every variable.

  • Variable a: 3000710, any other
  • Variable b: 3000700, [30, 33], any other
  • Variable c: 1, [4, 6], any other
  • Variable v: 1.0.9, 2.0.0, any other

Using the testing theory (equivalence partitioning and boundary value analysis) we can limit the above list of "critical" values.

[30, 33] => 30, 31, 33 (The value outside of this range is already covered by "any other")
[4, 6] => 4, 5, 6 (The value outside of this range is already covered by "any other". Though we did't really change anything in this case)

Nunit has an attribute [Combinatorial] which generates test cases for all possible combinations of the individual data items provided for the parameters of the test.

*Assumptions made: variables a, b, c are of type int, variable v is string

The code would look something like that:

        [Test, Combinatorial]        
        public void FirstTest(
            [Values(3000710, 0)] int a, 
            [Values(30, 31, 33, 3000700, 0)] int b,
            [Values(1, 4, 5, 6, 0)] int c, 
            [Values("1.0.9", "2.0.0", "")] string v)
        {
            RunTestMethod(a, b, c, v);
        }

You just need to store the generated test data on test execution

Upvotes: 1

Related Questions