Search code examples
continuous-integrationappdomainxunitcakebuildxunit2

AppDomain Usage Issue in Xunit test cases -Cake Script


I am running the Xunit Test cases as mentioned in my previous question.

How to get passed and fail test case count in xunit using cake(c# make) script

While running the test cases, most of my test cases are failed while trying to access a file from my AppDomain.

The test cases are succeeded in Visual Studio.

From the Error log, I can see that it tries to read the file from Xunit console runner location instead of application location.

Note: I am using NoAppDomain of Xunit2Settings as false.


Solution

  • When working with unit tests and files, I would recommend first to see if you can avoid using the actual filesystem by using data in memory or assembly resources. That way parallel execution, filesystem locks or similar environment related things won't be an issue. So if you have the opportunity to refactor the filesystem out of the tests (unless that is what your testing) I would go that route first.

    One way to work around the issue you're having is to use absolute paths, you could achieve this by in your tests utilizing the test assembly location and then Path.Combine the relative path to that. There's a good StackOverflow answer explaining on how to get the path of your assembly: https://stackoverflow.com/a/52956/5883153

    A quick fix you could try is using the Xunit2Settings WorkingDirectory to set same current dirrectory as VisualStudio, but that isn't something I've testest or recomend.