Visual Studio 2013 测试资源管理器
Visual Studio 2013 Test Explorer
我用 2 个项目创建了一个简单的 C#/.Net 解决方案:
- 一个Class图书馆。
- 具有单个 UnitTest 的单元测试项目 class,我已将 Moq 添加到该项目。
然后我创建了三个 UnitTest 方法,使用 Moq 模拟单个 class,然后将其注入另一个 class。
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace MockSimple.Test
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var mock = new Mock<ClassImplementingFunctionality>();
mock.Setup(functionality => functionality.Add(It.IsAny<int>(), It.IsAny<int>())).Returns(4);
var classUnderTest = new ClassUnderTest(mock.Object);
classUnderTest.Method(2,2);
mock.Verify(m => m.Add(It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(1));
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void TestMethod2()
{
var mock = new Mock<ClassImplementingFunctionality>();
mock.Setup(functionality => functionality.Add(It.IsAny<int>(), It.IsAny<int>())).Returns(0);
var classUnderTest = new ClassUnderTest(mock.Object);
classUnderTest.Method(10,5);
}
[TestMethod]
public void TestMethod3()
{
var ints = new List<int> {1, 2, 3, 4};
var mock = new Mock<ClassImplementingFunctionality>();
mock.Setup(functionality => functionality.Add(It.IsAny<int>(), It.IsAny<int>())).Returns(4);
var classUnderTest = new ClassUnderTest(mock.Object);
classUnderTest.LoopInts(ints);
mock.Verify(m => m.Add(It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(ints.Count));
}
[TestMethod]
public void TestMethod4()
{
var ints = new List<int> { 1, 2, 3, 4, -5, -2, -7 };
var mock = new Mock<ClassImplementingFunctionality>();
mock.Setup(functionality => functionality.Add(It.IsAny<int>(), It.IsAny<int>())).Returns(4);
var classUnderTest = new ClassUnderTest(mock.Object);
classUnderTest.LoopInts(ints);
mock.Verify(m => m.Add(It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(4));
}
}
}
但是当尝试 运行 使用 VS2013 中的内置测试管理器的测试方法时,我在测试输出 Window 中收到以下错误:
------ Run test started ------
The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
Value does not fall within the expected range.
Resulting in: An exception occurred while trying to create an instance of type 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection'.
Resulting in: Cannot activate part 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection'.
Element: 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection' --> Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection
Resulting in: Cannot get export 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection (ContractName="Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection")' from part 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection'.
Element: Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection (ContractName="Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection") --> Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection
========== Run test finished: 4 run (0:00:00,3464634) ==========*
我可以使用 Resharper 单元测试框架轻松 运行 测试方法。
对我来说,MSTest 似乎正在尝试进行一些依赖注入或寻找 MEF 或 Unity 配置。
有什么想法吗?
谢谢,谢谢...
in Developer Command Prompt run devenv /rootSuffix exp
上面的命令解决了问题:-)
我用 2 个项目创建了一个简单的 C#/.Net 解决方案:
- 一个Class图书馆。
- 具有单个 UnitTest 的单元测试项目 class,我已将 Moq 添加到该项目。
然后我创建了三个 UnitTest 方法,使用 Moq 模拟单个 class,然后将其注入另一个 class。
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace MockSimple.Test
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var mock = new Mock<ClassImplementingFunctionality>();
mock.Setup(functionality => functionality.Add(It.IsAny<int>(), It.IsAny<int>())).Returns(4);
var classUnderTest = new ClassUnderTest(mock.Object);
classUnderTest.Method(2,2);
mock.Verify(m => m.Add(It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(1));
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void TestMethod2()
{
var mock = new Mock<ClassImplementingFunctionality>();
mock.Setup(functionality => functionality.Add(It.IsAny<int>(), It.IsAny<int>())).Returns(0);
var classUnderTest = new ClassUnderTest(mock.Object);
classUnderTest.Method(10,5);
}
[TestMethod]
public void TestMethod3()
{
var ints = new List<int> {1, 2, 3, 4};
var mock = new Mock<ClassImplementingFunctionality>();
mock.Setup(functionality => functionality.Add(It.IsAny<int>(), It.IsAny<int>())).Returns(4);
var classUnderTest = new ClassUnderTest(mock.Object);
classUnderTest.LoopInts(ints);
mock.Verify(m => m.Add(It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(ints.Count));
}
[TestMethod]
public void TestMethod4()
{
var ints = new List<int> { 1, 2, 3, 4, -5, -2, -7 };
var mock = new Mock<ClassImplementingFunctionality>();
mock.Setup(functionality => functionality.Add(It.IsAny<int>(), It.IsAny<int>())).Returns(4);
var classUnderTest = new ClassUnderTest(mock.Object);
classUnderTest.LoopInts(ints);
mock.Verify(m => m.Add(It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(4));
}
}
}
但是当尝试 运行 使用 VS2013 中的内置测试管理器的测试方法时,我在测试输出 Window 中收到以下错误:
------ Run test started ------ The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information. Value does not fall within the expected range. Resulting in: An exception occurred while trying to create an instance of type 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection'. Resulting in: Cannot activate part 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection'. Element: 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection' --> Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection Resulting in: Cannot get export 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection (ContractName="Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection")' from part 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection'. Element: Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection (ContractName="Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection") --> Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection ========== Run test finished: 4 run (0:00:00,3464634) ==========*
我可以使用 Resharper 单元测试框架轻松 运行 测试方法。
对我来说,MSTest 似乎正在尝试进行一些依赖注入或寻找 MEF 或 Unity 配置。
有什么想法吗?
谢谢,谢谢...
in Developer Command Prompt run devenv /rootSuffix exp
上面的命令解决了问题:-)