我正在学习为银行应用程序创建单元测试方法,这是我的任务的一部分,而且我遇到了问题:
在Visual Studio上,由于某种原因未测试测试方法GetAccounts()
。我得到的输出消息是
"发现测试结束:0找到"
这是下面的测试方法块。
[TestMethod]
public void GetAccounts()
{
var testAccount = this.MockDatabase.GetAccounts();
Assert.IsNotNull(testAccount);
Assert.AreEqual(4, testAccount.Count);
}
如何让Visual Studio发现测试并给我一些结果?
如果有人要我发布更多我的代码,而不仅仅是上面的代码段让我知道。我很高兴为您提供更多信息。
答案 0 :(得分:1)
确保测试类还具有[TestClass]
属性
[TestClass] //<--- Test classes must have this attribute to discover test methods
public class AccountTests {
IDatabase MockDatabase;
[TestInitialize]
public void Arrange() {
var accounts = new List<Account>
{
new Checking( new Customer(1, "Alex", "Parrish"), 12, 30.00M ),
new Savings( new Customer(2, "Alex", "Russo"), 12, 29.00M ),
new Checking( new Customer(3, "Emma", "Swan"), 12, 30.00M ),
new Savings( new Customer(4, "Henry", "Mills"), 12, 30.00M )
};
var dataMock = new Mock<IDatabase>();
dataMock.Setup(_ => _.GetAccounts()).Returns(accounts);
//...code removed for brevity
MockDatabase = dataMock.Object;
}
[TestMethod]
public void GetAccounts() {
var testAccount = this.MockDatabase.GetAccounts();
Assert.IsNotNull(testAccount);
Assert.AreEqual(4, testAccount.Count);
}
//...code removed for brevity
}