Giter VIP home page Giter VIP logo

mstest-extensions's Introduction

VSTS Build Status

Microsoft Test Framework Extensions

This project provides extensions to the Microsoft Visual Studio Team Test unit testing framework. Features include alternatives to ExpectedExceptionAttribute and a fully extensible assertion application programming interface. The extensibility model is designed to allow swapping out the default Assert class and still maintain source code compatibility with the same intrinsic built-in functionality. You're then free to change, customize, or leverage the many built-in extensions.

Example Usage

To switch from a typical assertion model to the extensible assertion model, simply have your test class inherit from the UnitTest base class. This isn't a requirement to make things work, but it's the simplest model for consumption out-of-the-box.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
        
public class Person
{
    public Person( string name ) =>
        Name = name ?? throw new ArgumentNullException( nameof( name ), "The name cannot be null." );
            
    public string Name { get; }
}
        
[TestClass]
public class PersonTest : UnitTest
{
    [TestMethod]
    public void ConstructorShouldNotAllowNullName()
    {
        Assert.ThrowsIfArgumentNull( ( string name ) => new Person( name ) )
              .Verify( e => e.Message == "The name cannot be null." );
    }
}

If you don't want to use a base class, simply add a property to your existing test class:

[TestClass]
public class PersonTest
{
    Asserter Assert { get; } = new Asserter();

    [TestMethod]
    public void ConstructorShouldNotAllowNullName()
    {
        // arrange
        var person = new Person( "Bob" );

        // act
        var result = person.Name;

        // assert
        Assert.AreEqual( "Bob", result );
    }
}

Features

The following outlines the features and extensions provided out-of-the-box:

  • Asserter - Base assertion class and adapts over Assert for its default implemention
  • CollectionAsserter - Collection assertion class and adapts over CollectionAssert for its default implemention
  • ExceptionAsserter - Exception assertion class to further verify parameter names, messages, etc
  • IEnumerable<T> - Extension methods to verify various implementations of collections and sequences
  • INotifyPropertyChanged - Extension methods to verify implementations of the INotifyPropertyChanged interface
  • INotifyCollectionChanged - Extension methods to verify implementations of the INotifyCollectionChanged interface

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

mstest-extensions's People

Contributors

microsoft-github-policy-service[bot] avatar microsoftopensource avatar msftgits avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mstest-extensions's Issues

Assert.AreEqual breaks with MSTest v3

Hi,

When upgrading my test project to MSTest 3.0.2 I noticed some tests failing. I think there was a breaking change in microsoft/testfx#1433 . From what I can gather, mstest-extensions is still source compatible, but I suspect there's been a break in binary compatibility. If so, this should require a rebuild and rerelease of the NuGet package, right?

Here is a sample case:

<!-- .csproj file -->
<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<TargetFramework>net462</TargetFramework>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Microsoft.UnitTestFramework.Extensions" Version="2.0.0" />
		<PackageReference Include="MSTest.TestFramework" Version="3.0.1" />
		<PackageReference Include="MSTest.TestAdapter" Version="3.0.1" />
	</ItemGroup>

</Project>
[TestMethod]
public void TestObjectEquality()
{
    var target = new UnitTest();

    var assert = target.Assert;

    assert.AreEqual((object)5, (object)5);
}

The test output is:

 TestObjectEquality
   Source: UnitTest1.cs line 7
   Duration: 141 ms

  Message: 
Test method UnitTestTest.TestObjectEquality threw exception: 
System.MissingMethodException: Method not found: 'Void Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(System.Object, System.Object)'.

  Stack Trace: 
UnitTestTest.TestObjectEquality() line 14

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.