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

Time:08-12

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?

CodePudding user response:

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.

CodePudding user response:

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();
  • Related