我有一个项目需要一些UITest和UnitTest。如果我像下面的代码那样做的话。这样做会出现甚麽问题呢?或者使用小型孤立UITest和小型孤立UnitTest更好。也许已经有人面对过这种方法了。也可能是伟大的典范。我认为这种方法将是紧密耦合的,比项目更复杂。
namespace TestProject
{
class Program
{
static void Main(string[] args)
{
string commandLineArgs = string.Empty;
switch (commandLineArgs)
{
case "UI":
// Run library with UI on WPF
break;
case "SeriaApi":
// Run library without UI
break;
}
}
}
public interface ITest
{
void ConnectDevices(string controller1, string controller2);
}
public class ImplementationForUI : ITest
{
public void ConnectDevices(string controller1, string controller2)
{
// UI Test Realization, make the same that ImplementationForSerialApi.ConnectDevices with WPF wrapper
}
}
public class ImplementationForSerialApi : ITest
{
public void ConnectDevices(string controller1, string controller2)
{
// SerialApi Test Realization, make the same that ImplementationForUI.ConnectDevices but in console
}
}
public class Test
{
private ITest ITest;
public Test(ITest instance)
{
ITest = instance;
}
public void RunTestCase1()
{
ITest.ConnectDevices(null, null);
}
}
}发布于 2018-01-05 00:06:43
或者使用小型孤立UITest和小型孤立UnitTest更好。
是的,这样更好。
对于单元测试,您希望测试仍然可用的最小部分(即不包括实现细节)。
一旦您确定所有的部件都单独工作,您可能会有更多的测试,以确保部件正确地一起工作。在某种程度上,您已经验证了您的库是否工作。
现在,您对用户界面也做了同样的操作:模拟库并测试UI逻辑。您应该测试的内容以及应该测试的内容是一个全新的主题(您会发现这个主题已经在本页面中讨论过了)。
最后,剩下的就是启动(您的主要方法)。看到你这两条评论了吗?(使用UI运行库/运行没有UI的库)。应该用runLibraryWithUI();和runLibraryWithoutUI();来代替它们。
如果您正在执行两个以上的简单函数调用,请将这个启动序列分离出一个新模块并测试该模块。最后,您的主要方法应该非常简单,因此您不需要真正的测试。
发布于 2018-01-04 21:06:05
在我看来很丑。您不能将SeriaApi代码分离到一个单独的库中,其中包含它自己的单元测试,然后从您的主UI应用程序中使用这个库吗?
https://softwareengineering.stackexchange.com/questions/363433
复制相似问题