Developer
Developer

Reputation: 457

MSpec and Visual Studio 2017 integration

I would like to be able to run MSpec tests (specifications) from the Visual Studio 2017 Test Explorer. I have a .NET Framework 4.6.1 project, and I am using the following NuGet package: Machine.Specifications.Runner.VisualStudio. I see the tests listed in the Test Explorer. However, when I right-click and choose "Run selected tests", the tests disappear from the Test Explorer and are not run.

I have seen this older question dealing with the same problem in Visual Studio 2015. According to the blog post, the runner should support the Test Explorer. Or is it the case only when working with .NET Core projects?

I am wondering if the options are still:

or I am doing something wrong.

EDIT: I observe the behavior I have described only in case the specification is not implemented. The implemented specifications, regardless of whether they pass or fail, are visible in the Test Explorer. I assume this must be a bug or maybe expected behavior of the MSpec Visual Studio runner.

Upvotes: 0

Views: 703

Answers (1)

user2344354
user2344354

Reputation:

I'm not sure if this will help you but you can give it a shot.

I'm getting back to using MSpec after several years of not using it (I started using it over 10 years ago...) and was disappointed to see I couldn't run the tests directly in VSCode through the Test Explorer (using the .NET Core Test Explorer extension).

My solution is to create shims using xUnit that run the individual It delegates for each spec via the xUnit VSTest Adapter.

using Machine.Specifications;
using Machine.Specifications.Model;
using FluentAssertions;
using Xunit;

namespace Specs {

    [Subject("MSpec from Xunit")]
    public class When_yo_is_set_from_foo {

        public When_yo_is_set_from_foo() {
            context();
            of();
        }

        static int foo;
        static int yo;

        Establish context = () => 
            foo = 10;

        Because of = () =>
            yo = foo;

        It should_be_10 = () =>
            yo.Should().Be(10);

        [Fact]
        public void It_should_be_10() => should_be_10();
    }
}

This will let you run your tests in VS (code or full) then you can run the MSpec tests alone as part of your CI.

Tweeted here: https://twitter.com/kfinley/status/1111637434289319938

Upvotes: 0

Related Questions