-->

Shared Unit Tests with MSTest

2019-07-23 12:17发布

问题:

I have approximately 5-6 reports, they are structured the same, using Watin, I'm testing each one of these reports.

I have a shared test, I call "ReportBaseTests"..

    public class ReportBaseTests
    {
        public string MenuName { get; set; }

        public ReportBaseTests(string name)
        { this.MenuName = name; }

        [TestMethod]
        public void Perform_Invalid_Date_Range()
        {
        }
    }

but in each of my tests, I have...

    [TestClass]
    public class Report1Tests : ReportBaseTests
    {
        public Report1Tests()
            : base("Report 1")
        { }
    }

This works... each report will have a seperate Perform_Invalid_date_range, and it'll goto a different page... I was hoping someone had a better way to do this, as it also produces a seperate "non-runnable" test for the shared test since I didn't include the [TestClass]

Now, I know I could use NUnit and pass in arguments, however, I'm sticking with MSTest for the time being

回答1:

If you wanted, you could add TestContext support to your tests and have the ReportBaseTests.Perform_Invalid_Date_Range() parse the TestContext.FullyQualifiedTestClassName. For a simple test I think that's over kill.

For your solution: just put the [TestClass] attribute on ReportBaseTests and then mark ReportBaseTests as abstract. The "non-runnable" tests will disappear.