2

Inside my MsTests I have to reach and use a file in my project. So I am using Path.GetFullPath(@"......\name_of_file")

But when running the build I get a file not found error because the context of the path is the temp directory (C:\TeamCity\buildAgent\temp\buildTmp) instead of the checkout directory where all my files are (C:\TeamCity\buildAgent\work\e87255825b2f3eb1)

Is there a config to change that? Or a better way to do it?

thitemple
  • 5,833
  • 4
  • 42
  • 67

2 Answers2

2

I couldn't find a TeamCity option to specify the directory the unit tests should be run. Someone else might be able to help there.

However, I usually include files that are needed for the unit tests as embedded resources so that during execution you know for sure the file will always be accessible no matter where it is running from. You can easily get a Stream from an embedded resource and if you need to use an API that only takes a file name you can write the file to Path.GetTempPath().

Jonathon Rossi
  • 4,149
  • 1
  • 22
  • 32
0

I modified my tests to refer to the files like this:

string currentDir = new System.Diagnostics.StackFrame(true).GetFileName();
var workingFile = new FileInfo(currentDir);
string fileContents = File.ReadAllText(workingFile.Directory + @"\ResourceFolder\MyFile.xml");

I know this isn't a good way to do it, but this works for me locally and when called from TeamCity.

infojolt
  • 5,244
  • 3
  • 40
  • 82