配置 TeamCity NUnit 单元测试,以便可以正确加载文件
简而言之,我有一个在 IDE 中构建良好的解决方案,并且单元测试都可以在 NUnit GUI 上正常运行(通过 NUnitit VS2008 插件)。但是,当我执行 TeamCity 构建运行程序时,所有需要文件访问的单元测试(例如针对特定 XML 文件运行测试),我只会得到 System.IO.DirectoryNotFoundExceptions。
原因很明显:它正在错误的文件夹中寻找那些由各种单元测试加载的支持 XML 文件。
我的单元测试的结构方式如下所示:
+-- project folder
+-- unit tests folder
+-- test.xml
+-- test.cs
+-- project file.xaml
+-- project file.xaml.cs
我的所有项目都拥有自己的 UnitTests 文件夹,其中包含 .cs 文件以及运行测试所需的任何 XML 文件、XML 架构等。
因此,当我编写 test.cs 时,我让它在代码中查找“test.xml”,因为它们位于同一文件夹中(实际上,我做了类似 ....\unittests\test.xml 的操作,但那就是有点傻)。正如我之前所说,测试在 NUnit 中运行得很好。但那是因为单元测试是项目的一部分。从 TeamCity 运行单元测试时,我针对复制到主应用程序输出文件夹的程序集执行它们。不应仅仅为了使测试通过而将这些单元测试 XML 文件随意复制到输出文件夹。
谁能建议一种更好的方法来组织每个项目中的单元测试(它们是主应用程序的依赖项),以便我可以从 NUnit 和 从 TeamCity 构建运行程序执行单元测试?我能想到的唯一其他选择是将测试 XML 数据放入代码中,而不是从文件加载它。我宁愿不这样做。
In a nutshell, I have a solution that builds fine in the IDE, and the unit tests all run fine with the NUnit GUI (via the NUnitit VS2008 plugin). However, when I execute my TeamCity build runner, all unit tests that require file access (e.g. for running tests against specific XML files), I just get System.IO.DirectoryNotFoundExceptions.
The reason for this is clear: it's looking for those supporting XML files loaded by various unit tests in the wrong folder.
The way my unit tests are structured looks like this:
+-- project folder
+-- unit tests folder
+-- test.xml
+-- test.cs
+-- project file.xaml
+-- project file.xaml.cs
All of my projects own their own UnitTests folder, which contains the .cs file and any XML files, XML Schemas, etc that are necessary to run the tests.
So when I write my test.cs, I have it look for "test.xml" in the code because they are in the same folder (actually, I do something like ....\unit tests\test.xml, but that's kind of silly). As I said before, the tests run great in NUnit. But that's because the unit tests are part of the project. When running the unit tests from TeamCity, I am executing them against the assemblies that get copied to the main app's output folder. These unit test XML files should not be copied willy-nilly to the output folder just to make the tests pass.
Can anyone suggest a better method of organizing my unit tests in each project (which are dependencies for the main app), such that I can execute the unit tests from NUnit and from the TeamCity build runner? The only other option I can come up with is to just put the testing XML data in code, rather than loading it from a file. I would rather not do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有时,在发布问题之前先思考一下问题是最好的做法。我应该知道我应该只输入程序集自己的项目调试文件夹的相对路径。问题解决了。
Sometimes, sleeping on a question before posting it is the best thing to do. I should have known that I should just enter the relative paths of the assemblies' own project debug folder. Problem solved.