Teamcity - writing tests that access a separate JSON file

c# json unit-testing teamcity

457 просмотра

2 ответа

2718 Репутация автора

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.

Автор: Matt Источник Размещён: 18.07.2016 02:42

Ответы (2)


0 плюса

1082 Репутация автора

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();
    }
    ...
}
Автор: 56ka Размещён: 18.07.2016 02:47

1 плюс

57 Репутация автора

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.

Автор: Anthony Размещён: 18.01.2017 10:01
Вопросы из категории :
32x32