I am trying to run a set of test cases through one of our internal applications. I need to be able to automate these tests and run them through the command line and log various things. The problem is that the existing code does not separate the view from the controller and throws MessageBoxes and Alerts everywhere that ask the user to click on a button (in my case just OK/CONTINUE). Currently, it has been decided to introduce state flags that will help determine the context from which the application is being run so one can decide whether to do a Console.WriteLine()
or a MessageBox.Show()
. But as you can imagine this has cascaded into a lot of changes and dirty if-else blocks.
我试图通过我们的一个内部应用程序运行一组测试用例。我需要能够自动化这些测试并通过命令行运行它们并记录各种事物。问题是现有的代码没有将视图与控制器分开,并且在任何地方都会抛出MessageBoxes和Alerts,要求用户点击一个按钮(在我的例子中只是OK / CONTINUE)。目前,已经决定引入状态标志,这将有助于确定运行应用程序的上下文,以便可以决定是执行Console.WriteLine()还是MessageBox.Show()。但正如你可以想象的那样,这已经变成了许多变化和脏if-else块。
Sadly, it doesn't look feasible to sit down and separate the logic from the view at this stage. So I was wondering if there was a way to detect the context in which the application is executing. I would like to replace each of the MessageBox() call with a Notify() call which itself can detect the context - whether to show the output on a command prompt or pop up a form.
遗憾的是,在这个阶段坐下来将逻辑与观点区分开来看起来并不可行。所以我想知道是否有办法检测应用程序正在执行的上下文。我想用Notify()调用替换每个MessageBox()调用,该调用本身可以检测上下文 - 是否在命令提示符下显示输出或弹出表单。
EDIT_1: Any other suggestions for doing this are also welcome.
EDIT_1:欢迎任何其他建议。
2 个解决方案
#1
In C using the Win32 API (off the top of my head, untested):
在C中使用Win32 API(在我的头顶,未经测试):
// requires Windows 2000 or later (for GetConsoleWindow)
int parent_owns_the_console()
{
DWORD pid=0;
GetWindowThreadProcessId(GetConsoleWindow(), &pid);
return pid && pid != GetCurrentProcessId();
}
#2
In my application, I parse the command line arguments and set some internal flags. If you add that type of parsing to the entry point of your application, the existance of the command line argument being set to "Y" for example - can set the appropriate flag in the application used to determine where the output is sent.
在我的应用程序中,我解析命令行参数并设置一些内部标志。如果将这种类型的解析添加到应用程序的入口点,例如,命令行参数的存在设置为“Y” - 可以在用于确定输出发送位置的应用程序中设置适当的标志。
Personally, I capture all the "STDOUT" (console) output to a log file, then if not running in "scheduled" mode - I pop up a msgbox()
.
就个人而言,我将所有“STDOUT”(控制台)输出捕获到日志文件,然后如果没有以“预定”模式运行 - 我弹出一个msgbox()。
#1
In C using the Win32 API (off the top of my head, untested):
在C中使用Win32 API(在我的头顶,未经测试):
// requires Windows 2000 or later (for GetConsoleWindow)
int parent_owns_the_console()
{
DWORD pid=0;
GetWindowThreadProcessId(GetConsoleWindow(), &pid);
return pid && pid != GetCurrentProcessId();
}
#2
In my application, I parse the command line arguments and set some internal flags. If you add that type of parsing to the entry point of your application, the existance of the command line argument being set to "Y" for example - can set the appropriate flag in the application used to determine where the output is sent.
在我的应用程序中,我解析命令行参数并设置一些内部标志。如果将这种类型的解析添加到应用程序的入口点,例如,命令行参数的存在设置为“Y” - 可以在用于确定输出发送位置的应用程序中设置适当的标志。
Personally, I capture all the "STDOUT" (console) output to a log file, then if not running in "scheduled" mode - I pop up a msgbox()
.
就个人而言,我将所有“STDOUT”(控制台)输出捕获到日志文件,然后如果没有以“预定”模式运行 - 我弹出一个msgbox()。