Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to add assertion for each generated test by Intellitest

Here I am going to explain the issue by an example. The original question presents the problem more abstractly. No need to read it though.

Update: Question as an example

Lets say we have implemented this buggy function for finding the min of int[]:

public int MyMin(int[] data)
{
    int min = 1000;

    for (int i = 1; i < data.Length; i++)
    {
        if (data[i] < min)
        {
            min = data[i];
        }
    }

    return min;
}

Running Intellitest on this function gives us: enter image description here

Note for test #4 and #6 the function is not calculating the min value correctly due to its buggy implementation. However, these tests are passing which is not desired.

Intellitest cannot magically determine our intended behavior of MyMin and craft the test to fail on these inputs. However, it would be very nice if we could manually specify the desired result for these tests.

@michał-komorowski's solution is feasible, but for each test case I have to repeat its input in terms of PexAssumes. Is there a more elegent/clean way to specify the desired output for the test inputs?

Original Question

Intelitest generates a parameterized test that is modifiable and general/global assertions can be added there. It also generates the minimum number of inputs that maximize the code coverage. Intellitest stores the inputs as individual unit tests, each one calling the parameterized test with a crafted input.

I am looking for a way to add assertion per each input.

As each input is stored as a unit test function in a .g.cs file, the assertion can be added there. The problem is that these functions are not supposed to be customized by the user since they will get overwritten by Intellitest in its subsequent runs.

What is the recommended way of adding assertions for each unit test?

like image 270
Isaac Avatar asked Feb 09 '16 05:02

Isaac


People also ask

How do you write assertions in test cases?

Assert the exact desired behavior; avoid overly precise or overly loose conditions. One assertion, one condition. Don't aggregate multiple checks in one assertion. Write assertions that check requirements, not implementation details of the unit under test.

How many assertions should be included in a Junit test?

Proper unit tests should fail for exactly one reason, that's why you should be using one assert per unit test.

Which is the assertion tests?

In computer software testing, a test assertion is an expression which encapsulates some testable logic specified about a target under test.

How do you use IntelliTest?

Open your solution in Visual Studio and then open the class file that has methods you want to test. Right-click on a method and choose Run IntelliTest to generate unit tests for the code in your method. IntelliTest runs your code many times with different inputs.


1 Answers

You shouldn't add assertions to test methods (methods with [TestMethod] attribute). They are only used to provide parameters values. The place to put assertions are methods with [PexMethod] attribute.

At first glace it may look like a limitation. However, if we consider how IntelliTest works it isn't. There is no sense to add assertion per each input because inputs can be deleted, updated or created at any moment. For example when:

  • A method being tested was changed.
  • PexAssume class was used.
  • Configuration of PexMethod attribute was changed.

However, you can do something else i.e. to add more than one "Pex method" for a method being tested and use PexAssume. For example lets assume that we have BubbleSort method and we want to define different assertions depending on the length of an input array.

[PexMethod]
public void BubbleSort(int[] a)
{
    PexAssume.IsTrue(a.Length == 5);
    int[] result = Program.BubbleSort(a);
    // Assertions specific for an array with 5 elements
}

[PexMethod]
public void BubbleSort(int[] a)
{
    PexAssume.IsTrue(a.Length == 10);
    int[] result = Program.BubbleSort(a);
    // Assertions specific for an array with 10 elements
}
like image 103
Michał Komorowski Avatar answered Sep 29 '22 00:09

Michał Komorowski