C# 测试资源 [英] C# Test Resources

查看:20
本文介绍了C# 测试资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Microsoft Visual Studio 测试框架,并且我的一些测试需要一个 xml 文件作为输入.目前,我已将它们放在我的 TestProject 中的 Resource 目录下,并通过..\..\whatever"路径名称访问它们.我想在使用测试资源文件时获得良好实践的建议,因为我想在所有这些情况下重用我的测试- 直接来自 VS 2010- 在我在服务器上的构建过程中- 在持续集成环境中以透明(且简单)的方式.

I'm using Microsoft Visual Studio Test Framework and some of my tests require an xml file as input. Currently I've placed them under a Resource directory in my TestProject and I'm accessing them via a "..\..\whatever" path Name. I would like an advice for a good practice when using test resource files as I would like to reuse my test in all this situations - Directly from VS 2010 - During my build process on the server - In a Continuous Integration environment in a transparent (and simple) way.

推荐答案

首先,我通常会尝试在测试期间完全减少对文件系统的依赖(通过对文件系统引入某种抽象,因此可以模拟文件系统访问).

First of all I typically try to reduce the need to rely on the file system at all during testing (by introducing some sort of abstraction for the file system, so that the file system access can be mocked).

如果这是不可能的,我会将这些文件包含在测试项目中,位于名为 TestData 或类似的子文件夹中.然后我将复制到输出目录"设置为始终复制",以便文件包含在输出中,并且位于与测试程序集相关的已知位置(无论构建是否在 VisualStudio 或在构建服务器上).只要没有被测代码正在修改文件,而只需要它们来读取数据,这就会非常有效.

If that is not possible, I include such files in the test project, in a sub folder called TestData or something similar. Then I set the the "Copy to Output Directory" to "Always Copy", so that the file is included in the output, and at a location that is known in relation to the test assembly (regardless of whether the build is done inside Visual Studio or on a build server). This works very well as long as no code under test is modifying files, but only needs them for reading data.

这篇关于C# 测试资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆