简体   繁体   English

如何在Nunit中使用文件Asserts并检查文件是否存在

[英]How to use the file Asserts in Nunit and check the files are present or not

如何在Nunit中使用文件Asserts并检查文件是否存在。需要一些清晰和基本理解的例子。任何人都可以帮帮我

You should use: 你应该使用:

var fileName = @"C:\somedirectory\somefile.txt";

Assert.IsTrue(File.Exists(fileName));

FileAssert can be only used for comparison of two actual files from disc of from some abstract stream FileAssert只能用于比较来自某些抽象流的光盘中的两个实际文件

As of NUnit 2.9.7 (released just before this question was asked) you can use FileAssert.Exists(@"C:\\somedirectory\\somefile.txt"); 从NUnit 2.9.7开始 (在问这个问题之前发布)你可以使用FileAssert.Exists(@"C:\\somedirectory\\somefile.txt"); .

The new Framework allows this also: 新框架也允许这样做:

Ex. FileAssert.IsEqual(FileName1, FileName2);
    FileAssert.IsNotEqual(FileName1, FileName2);

To compare 2 Files. 比较2个文件。

May be help you as below: C# You need to deploy your files on your UT project. 可以帮到您,如下所示:C#您需要在UT项目中部署文件。

[DeploymentItem("file1.xml")] Copies file1.xml from the build output directory to the deployment directory. [DeploymentItem(“file1.xml”)]将file1.xml从构建输出目录复制到部署目录。 [DeploymentItem(@"Testfiles\\")] Copies all the files and folders in the Testfiles folder from the build output folder to the deployment folder. [DeploymentItem(@“Testfiles \\”)]将Testfiles文件夹中的所有文件和文件夹从构建输出文件夹复制到部署文件夹。 Subfolders are replicated in the deployment folder. 子文件夹在部署文件夹中复制。 [DeploymentItem("file2.xml", "DataFiles")] Creates a folder named DataFiles in the deployment folder, and copies file2.xml from the build output folder to DataFiles. [DeploymentItem(“file2.xml”,“DataFiles”)]在部署文件夹中创建名为DataFiles的文件夹,并将build2.xml从构建输出文件夹复制到DataFiles。

Assert.istrue(File.exist("your file")); Assert.istrue(File.exist(“your file”));

NUnit 3.0 includes a FileOrDirectoryExistsConstraint . NUnit 3.0包含FileOrDirectoryExistsConstraint

var filePath = "C:\pathtofile.txt";
Assert.That(filePath, Does.Exist);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM