我有一个测试项目,我需要加载一个XLSX文件.为此,我添加了一个带有copy-always的文件,以便它最终出现在build目录中,但以下所有内容都返回了错误的路径:
System.Reflection.Assembly.GetAssembly(typeof(testclass)).Location;AppDomain.CurrentDomain.BaseDirectoryDirectory.GetCurrentDirectory();他们都给了我:
"C:\\Users\\username\\Documents\\visual-studio-projecten\\projectname\\TestResults\\username_ICT003 2012-06-20 12_07_06\\Out"
我需要
"C:\\Users\\username\\Documents\\visual-studio-projecten\\projectname\\TestProject\\bin\\Debug\\SupportFiles\\"
我该如何做到这一点?
使用DeploymentItemAttribute属性.引用MSDN:
此属性标识包含要部署的测试运行的文件的文件和目录.测试引擎制作部署项的副本,并根据指定的OutputDirectory或缺省目录将它们放在测试部署目录中.
例如:
[TestClass]
public class MyUnitTest
{
[TestMethod()]
[DeploymentItem("myfile.txt")]
public void MyTestMethod()
{
string file = "myfile.txt";
Assert.IsTrue(File.Exists(file), "deployment failed: " + file +
" did not get deployed");
}
}
Run Code Online (Sandbox Code Playgroud)
当然假设您使用MSTest作为测试框架.
试试这个:
string dir = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"SupportFiles");
Run Code Online (Sandbox Code Playgroud)
不要使用,Directory.GetCurrentDirectory()因为当前目录不能是您的exe目录,并且可能在程序执行期间更改.
| 归档时间: |
|
| 查看次数: |
7902 次 |
| 最近记录: |