I have a simple C#
Class Calculate
which contains a Method Plus
:
public class Calculate
{
private int value1 = 2;
private int value2 = 4;
private int result;
public void Plus(int _value1, int _value2)
{
value1 = _value1;
value2 = _value2;
result = value1 + value2;
}
public int Result
{
get { return result; }
}
}
and generated a unit test as follows:
[TestClass()]
public class CalculateTests
{
[TestMethod()]
[Timeout(1000)]
public void PlusTest()
{
Calculate calc1 = new Calculate();
int counter = 0;
while (true)
{
counter++;
Random rnd = new Random();
int x1 = rnd.Next(0, 10);
int x2 = rnd.Next(0, 10);
calc1.Plus(x1,x2);
int expected = 10;
int actual = calc1.Result;
string message = string.Format("{0} + {1} = {2} at {3} repetion", x1, x2, actual, counter);
Assert.AreNotEqual(expected, actual, message);
}
}
This test checks if the summation of the two random integers is equal to 10. And it is repeated for 1 second.
The question is how should I set the timeout or modify the test so that if during the timeout the condition x1 + x2 == 10
does not happen, the test is considered as successfully performmed.