How should test cases using MSTest look like in C #?



  • Explain, please, with an example, what test cases are on the example of MS Test unit tests.

    The story begins with the fact that I received this remark: When testing, you should use test cases
    An example of how the tests look now (I do not insert testable methods by a specialist):

    [DataTestMethod, Description("Testing the properties of the vector lengths. Positive test result.")]
    [DynamicData(nameof(GetVectorsWithPositiveNumbersForLengthTest), DynamicDataSourceType.Method)]
    public void LengthProperty_OneVector_PositiveTestResult(Vector vector, double expectedLength)
    {
        Assert.AreEqual(expectedLength, vector.Length);
    }
    
    private static IEnumerable<object[]> GetVectorsWithPositiveNumbersForLengthTest()
    {
        yield return new object[] { new Vector(5, 1, 4), 22 };
        yield return new object[] { new Vector(10, 20, 30), 1310 };
        yield return new object[] { new Vector(6, 12, 9), 231 };
    }
    

    What will the following example look like if tested through test cases? Thanks!


  • Junior Test Automation

    MSTest uses the DataRow attribute to define test cases.
    Parameters are passed in it.
    A complex type - Vector in this case - is created inside a test method.

    [DataTestMethod]
    [DataRow(5, 1, 4, 22)]
    [DataRow(10, 20, 30, 1310)]
    [DataRow(6, 12, 9, 231)]
    public void LengthProperty_OneVector_PositiveTestResult(double x, double y, double z, double expectedLength)
    {
        var vector = new Vector(x, y, z);
    
        Assert.AreEqual(expectedLength, vector.Length);
    }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2