简体   繁体   中英

What is the idiomatic way to assert that a file exists in xunit?

I need to assert that a file exists in an XUnit unit test. I do not want to use the generic Assert.True(File.Exists(...)); assertion. In NUnit we have FileAssert.Exists . How are we supposed to do it in XUnit?

There is no idiomatic way to do file existence checks. Just assert true using File.Exists as you expected. The xunit documentation is pretty poor about describing the API's suggested usage, but you can browse the assert namespace in the xunit repo and see that there are no file specific asserts that exist.

All depends on style preference, but some options are:

  1. use Assert like you say
  2. create your own FileAssert helper class
  3. use FluentValidations ( https://fluentassertions.com/ )

I've personally been steering more towards FluentAssertions, so in this example it would be:

File.Exists("foo.txt").Should().BeTrue();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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