对 Windows 计划任务控制台应用程序进行单元测试
我正在接管一个包含控制台应用程序项目的遗留系统,该项目在我们的生产服务器上作为计划任务运行。它主要生成每日和每周报告,进行一些数据库维护等。
控制台应用程序的“主要”处理输入命令行参数并确定要执行几个不同进程中的哪一个。类似于
Module MainModule
Public Sub Main()
'--- Check if command line arguments were specified
Dim args() As String = Environment.GetCommandLineArgs()
If args.Length > 1 Then
ConsoleMain(args)
End If
End Sub
Public Sub ConsoleMain(ByVal args() As String)
Dim rc As New Coordinator(enableEmails_)
Try
Dim arg_ As String = args(1)
Dim success_ As Boolean = True
Select Case arg_.ToUpper()
Case TaskType.OrderIntegration
success_ = rc.OrderIntegration()
Case TaskType.Motivators
success_ = rc.MotivatorsCreateFile(New CustomerMotivatorsManager)
... repeat for each of the various "Task Types"
End Module
我的问题是: - 这是一个带有 Main() 和 ConsoleMain() 的控制台应用程序,我似乎没有可以通过测试访问的任何内容 - 即“Main”和“ConsoleMain”似乎无法访问。我如何对这样的东西进行单元测试,以测试“如果传递参数'x',则调用函数'y'”?
提前致谢,
I am in the process of taking over a legacy system that contains a Console App project, which is run as a Scheduled Task on our production servers. It mainly produces daily and weekly reports, does some database maintenance, etc.
The "main" of the console app handles inputting the command line arguments and determining which of several different processes to execute. Something like
Module MainModule
Public Sub Main()
'--- Check if command line arguments were specified
Dim args() As String = Environment.GetCommandLineArgs()
If args.Length > 1 Then
ConsoleMain(args)
End If
End Sub
Public Sub ConsoleMain(ByVal args() As String)
Dim rc As New Coordinator(enableEmails_)
Try
Dim arg_ As String = args(1)
Dim success_ As Boolean = True
Select Case arg_.ToUpper()
Case TaskType.OrderIntegration
success_ = rc.OrderIntegration()
Case TaskType.Motivators
success_ = rc.MotivatorsCreateFile(New CustomerMotivatorsManager)
... repeat for each of the various "Task Types"
End Module
What my question is:
- This being a Console App with a Main() and ConsoleMain(), I don't seem to have anything that I can access from a Test - i.e. the "Main" and "ConsoleMain" do not appear to be accessible. How can I unit-test something like this, to test the "if argument 'x' is passed, function 'y' is called"?
Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定为什么 Main 在您的测试中不可见,除非 VB.NET 做了一些幕后工作来隐藏它。
无论如何,为什么不将您的代码移到它自己的类中呢?然后,您可以一次针对每个类运行单元测试,而不是一次执行整个测试。
单元测试通常针对各个类执行,而不是执行应用程序的主入口点。
I'm not sure why Main wouldn't be visible from your tests, unless VB.NET does some behind-the-curtains stuff to hide it.
In any case, why not move your code into its own class(es)? Then you can run unit tests against each class at a time, rather than executing the whole thing at once.
Unit tests usually execute against individual classes, rather than executing the Main entry point of an app.