NUnit - 读写文件的程序集
我正在对一个使用 File.WriteAllLines() 和 File.ReadAllText() 读取和写入持久数据的程序集进行单元测试。当我使用 NUnit Gui 运行单元测试时,测试失败并出现未经授权的AccessException。
文件尝试读写的路径当然会受到程序执行位置的影响。根据我是在 Visual Studio 下运行 NUnit 还是直接从 Nunit /bin 文件夹运行 NUnit,这就是尝试进行读写操作的位置。
我尝试以管理员身份运行 NUnit 并将所有程序集直接复制到 NUnit /bin 文件夹,但测试仍然失败。
我不想直接在程序集中设置路径。
关于如何解决这个问题有什么想法吗?
I am unit testing an assembly that uses File.WriteAllLines() and File.ReadAllText() to read and write persistent data. When I run the unit test with the NUnit Gui the test fails with an unauthorizedAccessException.
The path that the file is attempting to read and write is being affected of course by the location of program execution. Depending on if I run NUnit under Visual Studio or directly from the Nunit /bin folder, this is where the attempted read and write operation is taking place.
I have tried to run NUnit as administrator and have copied all of the assemblies directly to the NUnit /bin folder and the test still fails.
I do not want to directly set a path in the assembly.
Any ideas on how to resolve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 NUnit 3,您应该使用 TestContext 来获取 TestDirectory 或 WorkDirectory。
还有文件断言 和 目录断言 ,这可以帮助您以更具可读性的方式编写测试。
With NUnit 3 you should use TestContext to get either TestDirectory or WorkDirectory.
Also there are File Asserts and Directory Asserts, which may help you write your tests in a more readable way.
您可以使用
Path.GetTempFileName
获取要写入的临时文件。或者,您可以使用
Assembly.Location< /code>
找出程序集所在的位置,并将其用作目录。
You could use
Path.GetTempFileName
to get a temporary file to write to.Alternatively, you could use
Assembly.Location
to find out where your assembly is, and use that as the directory.