6
\$\begingroup\$

Background:

Didn't what to have to reference bulky 3rd party lib for inline data driven tests and wanted to use standard MS testing framework.

Created helperInlineDataDrivenTest to provide a workaround for this limitation.

Usage

[TestClass]public class DataDrivenTests : MiscUnitTests {    [TestMethod]    public void Should_Do_Data_Driven_Test() {        //Arrange         var source = new TestDataSource();        var test = InlineDataDrivenTest.Create(source);        test.Setup(c => c.TestAdd(0, 0, 0));        //Act        test.Run();        //Assert        var summary = test.GetResult();        Assert.IsTrue(summary.Succeeded, summary.Message);    }    [TestMethod]    public void Should_Do_Data_Driven_Test_And_Fail() {        //Arrange         var target = new TestDataSource();        var test = InlineDataDrivenTest            .Create(target)            .Setup(m => m.TestSubtract(0, 0, 0));        //Act        test.Run();        //Assert        var summary = test.GetResult();        Assert.IsTrue(summary.Failed == 1, summary.Message);    }    public class TestDataSource {        [DataDrivenTestMethod]        [DataRow(1, 2, 3)]        [DataRow(4, 5, 9)]        public void TestAdd(int a, int b, int expected) {            Calculator c = new Calculator();            int actual = c.Add(a, b);            Assert.AreEqual(expected, actual);        }        [DataDrivenTestMethod]        [DataRow(4, 2, 2)]        [DataRow(4, 1, 1, DisplayName = "Should fail for demo purposes")]        public void TestSubtract(int a, int b, int expected) {            Calculator c = new Calculator();            int actual = c.Subtract(a, b);            Assert.AreEqual(expected, actual);        }    }    public class Calculator {        public int Add(int a, int b) {            return a + b;        }        public int Subtract(int a, int b) {            return a - b;        }    }}

Thoughts and feed back on code.

This solves the immediate need but I'm looking to improve on current model.

askedApr 21, 2016 at 16:01
Nkosi's user avatar
\$\endgroup\$
3
  • \$\begingroup\$I'd be interesting in seeing the implementation of your attributes.\$\endgroup\$CommentedApr 22, 2016 at 8:03
  • \$\begingroup\$They are just simple custom classes derived fromSystem.Attribute used to provide metadata. At runtime during testing the target class is inspected using reflection for those attributes then tagged methods are invoked with provided data and results aggregated.\$\endgroup\$CommentedApr 22, 2016 at 12:17
  • \$\begingroup\$nice. How can I try this ?\$\endgroup\$CommentedJun 13, 2017 at 6:56

0

You mustlog in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.