C# 测试驱动开发
C# Test Driven Development
我正在尝试使用 C# 执行一些简单的测试驱动开发测试。但是我对此没有真正的经验并且遇到了一些麻烦。
我要进行的测试是登录验证。以下是我目前所拥有的。
using System;
namespace TimetableSystem
{
public class Timetable
{
public bool TimetableLogin(string username, string password)
{
throw new NotImplementedException();
}
}
}
测试方法
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TimetableSystem;
namespace TimetableSystemTests
{
[TestClass]
public void TestLoginMethodValid()
{
Timetable auth = new Timetable();
bool result = auth.TimetableLogin("Name", "Password");
Assert.IsTrue(result);
}
}
此测试一直失败,我不确定为什么要感谢您的帮助。
测试失败,因为您尚未实现登录方法(请参阅 NotImplementedException),这是测试驱动开发中的设计。您编写了足够多的代码以便方法和测试在不实现它的情况下编译,然后您开始实现您的方法以便测试通过。
将登录服务注入您的时间表class:
public interface ILoginService
{
bool Login(string username, string password);
}
public class TimeTable
{
ILoginService _loginService;
public TimeTable(ILoginService loginService)
{
_loginService = loginService;
}
public bool TimeTableLogin(string username, string password)
{
return loginService.Login(username, password);
}
}
然后在您的测试项目中创建一个模拟作为您的登录服务的实现。此版本服务仅供测试。
public MockLoginService : ILoginService
{
public bool Login(string username, string password)
{
return (username == "Name" && password == "Password");
}
}
然后在测试中:
[TestMethod]
public void TestLoginMethodValid()
{
MockLoginService mockLoginService = new MockLoginService();
Timetable auth = new Timetable(mockLoginService);
bool result = auth.TimetableLogin("Name", "Password");
Assert.IsTrue(result);
}
现在您正在测试 TimeTable.TimetableLogin 中的逻辑,而不是登录服务。接下来,您将实现要在生产中使用的 ILoginService 的真实版本,您可以确信 TimeTable.TimetableLogin 将按预期执行。
我正在尝试使用 C# 执行一些简单的测试驱动开发测试。但是我对此没有真正的经验并且遇到了一些麻烦。
我要进行的测试是登录验证。以下是我目前所拥有的。
using System;
namespace TimetableSystem
{
public class Timetable
{
public bool TimetableLogin(string username, string password)
{
throw new NotImplementedException();
}
}
}
测试方法
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TimetableSystem;
namespace TimetableSystemTests
{
[TestClass]
public void TestLoginMethodValid()
{
Timetable auth = new Timetable();
bool result = auth.TimetableLogin("Name", "Password");
Assert.IsTrue(result);
}
}
此测试一直失败,我不确定为什么要感谢您的帮助。
测试失败,因为您尚未实现登录方法(请参阅 NotImplementedException),这是测试驱动开发中的设计。您编写了足够多的代码以便方法和测试在不实现它的情况下编译,然后您开始实现您的方法以便测试通过。
将登录服务注入您的时间表class:
public interface ILoginService
{
bool Login(string username, string password);
}
public class TimeTable
{
ILoginService _loginService;
public TimeTable(ILoginService loginService)
{
_loginService = loginService;
}
public bool TimeTableLogin(string username, string password)
{
return loginService.Login(username, password);
}
}
然后在您的测试项目中创建一个模拟作为您的登录服务的实现。此版本服务仅供测试。
public MockLoginService : ILoginService
{
public bool Login(string username, string password)
{
return (username == "Name" && password == "Password");
}
}
然后在测试中:
[TestMethod]
public void TestLoginMethodValid()
{
MockLoginService mockLoginService = new MockLoginService();
Timetable auth = new Timetable(mockLoginService);
bool result = auth.TimetableLogin("Name", "Password");
Assert.IsTrue(result);
}
现在您正在测试 TimeTable.TimetableLogin 中的逻辑,而不是登录服务。接下来,您将实现要在生产中使用的 ILoginService 的真实版本,您可以确信 TimeTable.TimetableLogin 将按预期执行。