如果您不想更改任何测试代码(即不想在顶部添加别名),这个 shim 对我有用:
using System;
using System.Collections;
namespace Microsoft.VisualStudio.TestTools.UnitTesting
{
public class Placeholder{}
public class TestClassAttribute : NUnit.Framework.TestFixtureAttribute
{
}
public class TestInitializeAttribute : NUnit.Framework.SetUpAttribute
{
}
public class TestMethodAttribute : NUnit.Framework.TestAttribute
{
}
public class TestCleanupAttribute : NUnit.Framework.TearDownAttribute
{
}
public class IgnoreAttribute : NUnit.Framework.IgnoreAttribute
{
}
public class ExpectedExceptionAttribute : NUnit.Framework.ExpectedExceptionAttribute
{
public ExpectedExceptionAttribute(Type exceptionType) : this(exceptionType, null)
{
}
public ExpectedExceptionAttribute(Type exceptionType, string message) : base(exceptionType)
{
UserMessage = message;
}
}
public class TestContext : NUnit.Framework.TestContext
{
public TestContext(IDictionary dictionary) : base(dictionary)
{
}
}
public class Assert : NUnit.Framework.Assert
{
public static void IsInstanceOfType(object obj, Type type)
{
NUnit.Framework.Assert.IsInstanceOfType (type, obj, null);
}
public static void IsInstanceOfType(object obj, Type type, string message)
{
NUnit.Framework.Assert.IsInstanceOfType (type, obj, message);
}
}
public class CollectionAssert : NUnit.Framework.CollectionAssert
{
}
}
这对我来说可以通过 NUnit 运行 MSTest(至少在 Xamarin Studio 的单声道下)。只需包含该文件并正确获取引用(您可能需要不同的项目文件或条件引用),就可以了。