Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Is there a way to share assertions among test classes without inheritance in NUnit?</p> </blockquote> <p>Considering you just want to share assertions and you're testing for behaviors on interfaces, you could just create something simple:</p> <pre><code>public interface IBehavior { void Check(); } public class VehicleThatHasBeenStartedBehaviors : IBehavior { protected IVehicle vehicle; public VehicleThatHasBeenStartedBehaviors(IVehicle vehicle) { this.vehicle = vehicle; } public void Check() { Assert.That(vehicle.IsEngineRunning, Is.True); Assert.That(vehicle.RevCount, Is.LessThanOrEqualTo(1000)); Assert.That(vehicle.RevCount, Is.GreaterThanOrEqualTo(0)); } } [TestFixture] public class when_starting_a_car { protected Car car; [SetUp] public void SetUp() { car = new Car(); } [Test] public void behaves_like_a_started_vehicle() { new VehicleThatHasBeenStartedBehaviors(car).Check(); } } </code></pre> <p>BUT, if you want to specifically use MSpec syntax, I think you may have to implement it or find some framework that does it for you.</p> <p><strong>EDIT</strong></p> <p>Reading your comments on the question I realized you may be wanting to reuse test methods, and not simply assertions. If that's the case, you could write a <a href="http://www.nunit.org/index.php?p=nunitAddins&amp;r=2.6.1" rel="nofollow">customized addin for NUnit</a>. That may sound a little overkill, but it's up to you to decide. </p> <p>My <strong>starting point</strong> would be writing a customized <a href="http://www.nunit.org/index.php?p=suiteBuilders&amp;r=2.6.1" rel="nofollow">SuiteBuilder</a> object:</p> <blockquote> <p><strong>Purpose</strong></p> <p>A SuiteBuilder is an addin used to build a test fixture from a type. NUnit itself uses a SuiteBuilder to recognize and build TestFixtures.</p> </blockquote> <p>Using your own suite builder you could read some behavior classes and compose your fixture.</p> <pre><code>[NUnitAddin] public class TestCompositionAddinProvider : IAddin { #region IAddin Members public bool Install(IExtensionHost host) { IExtensionPoint builders = host.GetExtensionPoint("SuiteBuilders"); if (builders == null) return false; builders.Install(new TestCompositionSuiteBuilder()); return true; } #endregion } public class TestCompositionSuiteBuilder : ISuiteBuilder { #region ISuiteBuilder Members public bool CanBuildFrom(Type type) { //TODO: Create validation logic return true; } public NUnit.Core.Test BuildFrom(Type type) { if (CanBuildFrom(type)) return new ComposedSuiteExtension(type); return null; } #endregion } public class ComposedSuiteExtension : TestSuite { public ComposedSuiteExtension(Type fixtureType) : base(fixtureType) { //TODO: Create logic to add test methods from various behaviors. } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload