使用 MSBuild 将参数传递给 MSTests
我有一个问题想知道是否可以通过特定方式解决。
我希望能够在将在 TeamBuild 服务器上运行的 MSBuild 脚本中传递参数或设置某种变量。
该参数将用作 MSTest 中 TestFixture 设置的条件,以决定使用类的哪个具体实现。 当在构建服务器上运行时,它将是一个模拟版本;当在开发人员计算机上运行时,它将是对物理资产的引用。
这容易实现吗? 我可以设置一个环境变量,但更喜欢 MSTest 和 MSBuild 中是否有可以使用的特定内容。
I've got an issue that was wondering if could be solved in a particular way.
I would like to be able to pass a parameter or set some kind of variable in an MSBuild script that will be run on a TeamBuild server.
This parameter would be used as a condition in the setup of a TestFixture in MSTest to decided which concrete implementation of a class to be used. It would be a mock version when running on the build server, and a reference to a physical asset when running on a developer machine.
Is this easily possible? I could set an environment variable but would prefer if there was something specific in MSTest and MSBuild that could be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现最简单的方法是编写配置文件。 MsBuild 社区任务使这成为可能。
The easiest way to do this that I have found is to write configuration files. There are MsBuild community tasks that make this possible.
作为 xUnit 准则,测试不应接受参数。 它们应该直接运行,无需有人配置它们。
您的需求似乎更倾向于依赖注入。 像 Spring.Net 这样的框架更适合。
更新:
从您的评论来看,您似乎只需要一个类似于 #define BUILD 的开关。 尝试条件编译符号(项目设置>构建)与 ReplaceCollaboratorsForBuildServer 方法相结合,该方法用 ConditionalAttribute 并在 testFixture 设置方法末尾调用。
As a xUnit guideline, tests should not take in parameters. They should just run without someone having to configure them.
Your need seems to be more towards dependency injection. For which frameworks like Spring.Net are a better fit.
Update:
From your comment, it seems all you require is a switch similar to a #define BUILD. Try Conditional Compilation symbols (Project Settings>Build) coupled with a ReplaceCollaboratorsForBuildServer method that is decorated with the ConditionalAttribute and called at the end of your testFixture Setup method.