VB.net NUnit (2.5) Windows 窗体测试 - 这可能吗

发布于 2024-12-10 13:56:29 字数 744 浏览 0 评论 0原文

我正在将单元测试改造到一个由其他开发人员在 VB.net 中设计和编写的相当复杂的系统中。我尝试使用 NUnit 和 NUnit Forms 扩展为 GUI 表单开发单元测试。 (我一直在研究 C# 示例,如果您有解决方案但不知道 VB 语法,只要它使用 NUnit 类,这些示例就很容易移植)

我将尝试解释我正在做什么,但首先是一个简短的描述的程序。它主要监视服务器活动。您需要通过带有 IP 和端口字段(等等)的模态表单连接到服务器。一旦连接到服务器,程序的其他部分就会解锁并变得可用(例如服务器的配置)。

所需过程:加载程序>单击连接按钮>模态连接表单负载>输入详细信息>点击确定连接>主窗体更新为登录状态>其他功能

问题是我无法测试连接表单的功能以及程序的登录功能。我可以测试它是否正确加载模式连接表单;输入详细信息并单击“确定”(到目前为止一切正常),但它似乎没有逻辑上推进程序。模态表单似乎再次关闭,而无需从程序后端运行连接代码,我回到主菜单,未登录任何内容。

我有一种感觉,我要么错过了一些非常明显的东西,要么它在 NUnit 中根本不可行。我在互联网上搜寻类似的东西,但最接近的是另一个非常通用的SO线程。由于无法实际测试该程序的登录版本,我遇到了一个主要障碍。 另一个问题是处理没有唯一标识符的消息框(例如“您确定要退出吗?”);这些似乎也是 NUnit 的一大难题

,将测试作为独立项目运行)

(如果有什么区别,我将使用对构建项目的可执行文件的引用,而不是实际源代码 如果需要的话,发布一些我的测试代码。

I am retrofitting unit testing into a fairly complex system designed and written by other developers in VB.net. I am trying to develop unit tests for the GUI forms using NUnit and the NUnit Forms extension. (I've been looking at c# examples that are fairly easy to port over if you have a solution but don't know VB syntax as long as it uses NUnit classes)

I will try and explain what I am doing but first a brief description of the program. It basically monitors server activity. You need to connect to a server via a modal form with IP and Port fields(amongst others). Once you have connected to a server other parts of the program unlock and become usable (such as configuration of the server).

Desired process: Load program > click connect button > modal connect form loads > enter details > click OK to connect > main form updates to logged-in state > other functionality

The problem is that I cannot test the functionality of the connect form and then the logged-in functionality of the program. I can test that it loads the modal connect form correctly; enters the details and clicks OK (all fine so-far) but it does not appear to logically progress the program. The modal form just closes again seemingly without running the connect code from the program back-end and I’m back at the main menu not logged in to anything.

I have a feeling that I’ve either missed something really obvious or that it’s simply not doable in NUnit. I have trawled the internet in search of anything similar but the closest was another SO thread that was really generic. Without being able to actually test the logged-in version of the program, I'm at a major hurdle.
Another issue is handling message boxes that don’t have unique identifiers (e.g. “are you sure you want to exit?”); these also seem to be a major pain in the arse with NUnit

(If it makes any difference, I’m running the tests as a stand-alone project using a reference to the executable file of the built project, not the actual source)

Can post some of my testing code if required.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言 2024-12-17 13:56:29

恕我直言,使 GUI 类可用于单元测试的最佳方法是应用 模型-视图-Presenter 模式 并将几乎每个程序逻辑从表单 (=View) 类中分解到一个单独的 Presenter 类。然后,您可以对 Presenter 类进行单元测试,而无需使用“NUnit Forms”等工具。

请阅读 Michael Feathers 的文章“The Humble Dialog Box” 以获取 C++ 示例,我想你可以轻松地将其应用到 Winforms 中。

IMHO the best approach to make GUI classes feasible for unit tests is to apply the Model-View-Presenter pattern and factor almost every program logic out of the form (=View) class to a separate Presenter class. Then you can unit test the Presenter class without the need for tools like "NUnit Forms".

Read Michael Feathers' article "The Humble Dialog Box" for an example in C++, you can easily apply that to Winforms, I guess.

丶视觉 2024-12-17 13:56:29

我不确定 NUnit 表单,但使用 White 库(也适用于 NUnit),您'我们能够通过运行 exe 并模仿用户操作来测试应用程序。应用程序正常运行,因此所有应用程序逻辑均已执行。

以下是使用 White 启动应用程序的一些示例代码:

Dim app = White.Core.Application.Launch("MyApp.exe")

从应用程序访问表单:

Dim mainForm = app.GetWindow(SearchCriteria.ByAutomationId("MainForm"), 
                             InitializeOption.NoCache)

执行操作,例如单击菜单项:

mainForm.MenuBar.MenuItem("Edit", "Jobs...").Click()

获取控件并验证其状态:

Dim someTextBox = mainForm.Get(Of TextBox)(SearchCriteria.ByAutomationId("txtValue"))
Assert.IsTrue(someTextBox.Text = "12345")

我不确定 NUnit Forms 是否具有类似的功能,但如果不,也许你应该看看怀特。我在设置它时遇到了一些问题,因此请确保在设置之前仔细阅读文档(不幸的是不是很详尽)。

I'm not sure about NUnit forms, but using the White library (which also works with NUnit), you're able to test the application by running the exe and mimicking user actions. The application runs normally so all application logic is performed.

Here's some example code for launching an app with White:

Dim app = White.Core.Application.Launch("MyApp.exe")

Accessing a form from your app:

Dim mainForm = app.GetWindow(SearchCriteria.ByAutomationId("MainForm"), 
                             InitializeOption.NoCache)

Performing an action such as clicking a menu item:

mainForm.MenuBar.MenuItem("Edit", "Jobs...").Click()

Getting a control and validating its state:

Dim someTextBox = mainForm.Get(Of TextBox)(SearchCriteria.ByAutomationId("txtValue"))
Assert.IsTrue(someTextBox.Text = "12345")

I'm not sure if NUnit Forms has similar capabilities, but if not, maybe you should look into White. I ran into some issues setting it up so make sure to read the documentation carefully (not very exhaustive unfortunately) before setting it up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文