简体   繁体   中英

split test definition and test implementation with nunit, xunit or visual studio test

I'd like to split the definition of test an its implementation like following:

 [TestFixture] public abstract class ContainerTests { [Test] public abstract void BuildContainer(); } public class UnityTest : ContainerTests { public override BuildContainer() { // Implementation } } 

I'd like to have an abstract definition of scenarios and implement it in differnt kind so I can compare them. The TestRunner have to catch all tests from the inherited class even there are definied in the baseclass. Is there any way to do this?

Best regards, zimmy

with xunit you can happily do :-

 public abstract class ContainerTests
    {
        [Fact]
        public abstract void BuildContainer();
    }

    public class UnityContainer : ContainerTests
    {
        public override void BuildContainer()
        {
            Assert.Equal(1,2);
        }

    }

If it were possible to write the code you give in NUnit, it would mean that every developer of a derived class would have to have a test with the name BuildContainer . That test could test anything at all, or even just be empty. So if your intention is to force devs to implement a test that tests something particular, then this won't do the job.

What you can do, however, is implement the test in the base class using calls to specific abstract methods that the derived class would have to implement. In general, you would want those methods to do something you can assert on in the base class rather having them perform the required asserts yourself.

In general, the lack of ability to override test methods in NUnit is due to a view by the authors that it isn't such a good idea in most cases. I'd be interested to hear of a case that's a counter-example though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM