2
votes

I have TeamCity running for a C# project. The Unit tests are written using MSTest and they include an external JSON file. They are loaded in because they're large and I don't want to have to escape them in C#.

I import them like this:

[TestInitialize]
public void Setup()
{
  using (StreamReader r = new StreamReader(@".\currency2.json"))
  {
    _json = r.ReadToEnd();
  }
  ...

They run fine locally. I have 'Copy always set' but when the tests are ran using Teamcity I get an error saying that it can't find them in a temp folder. They are copied over to the build server but they're not in this temp folder.

Could not find file 'E:\TeamCity\buildAgent\temp\buildTmp\SYSTEM_SERVER 2016-07-18 15_28_19\Out\currency2.json'

I have **\bin\release\*test*.dll setup as my Test File Names in the test build step.

Any help appreciated.

2
Is it normal that the error message is about currency2.json and your code sample about currency1.json. Maybe, currency1.json is set to copy local, but not currency2.json ?Didier Aupest
Sorry that is a little misleading. Typo. It can't find any of the files.Matt

2 Answers

1
votes

I had a similar problem.

I changed the properties of the test file to this

  • Build Action = Content
  • Copy to Output Directory = Copy always

Teamcity will copy the file to the build folder, but it does not seem to maintain the same structure you'd expect.

So I created a file lookup loop. That will step down the expected folder until it finds the text file in question.

 var pathLookups = new string[]
        {
            "2ndFolder\\3rdFolder\\test.json", // folder that normally workes
            "3rdFolder\\test.json",
            "test.json"
        };
        foreach (var pathLookup in pathLookups)
        {
            try
            {
                jsonFileCollection = JsonFileLoader<TestJsonType>.LoadJson(pathLooksup);
                if (jsonFileCollection!= null)
                {
                    break;
                }
            }
            catch (Exception)
            {

                Console.WriteLine("Attempted to load test json from path:" + pathLooksup);
            }
        }

It's not the cleanest solution, but it will get the job done. You could refactor this to look a little nicer.

0
votes

You might pass the full pass by argument to your program (and value defined in TeamCity).

Something like this (this is a pseudo-code example only) :

string[] programArgs;
static void Main(string[] args)
{
    programArgs = args
}

[TestInitialize]
public void Setup()
{
    using (StreamReader r = new StreamReader(programArgs[1]))
    {
    _json = r.ReadToEnd();
    }
    ...
}