Test projects not reading app.config in TeamCity -

2020-08-10 07:51发布

问题:

Well we are facing a strange problem with JetBrains TeamCity induced unit tests on our main project where tests from few library projects are failing regularly. Apparently, it's not reading the config file (coming from app.config and nicely stored in project -> bin -> debug -> projectName.dll.config).

Hints or tips on what could be the real issue would be highly appreciated.

回答1:

I've got the same problem and wasted a couple of hours to figure out what the problem is.

In our case, the NUnit plugin was configured to run the tests from:

**\*Tests.dll

Though this sounds to be OK, it has turned out that this pattern will not only match to the MyTests.dll in the bin\Debug folder but also to the obj\Debug\MyTests.dll. The obj folder is used internally for the compilation and does not contain the config file.

Finally the solution was to change the plugin configuration to

**\bin\Debug\*Tests.dll

Actually we use a system variable for the build configuration so we did not have the "Debug" hard-coded. Using bin* might be also dangerous when the workspace is also used for Debug/Release builds and you don't have a full cleanup specified.

You might wonder why I did not realize the test count mismatch (actually it was doubled, because they were running once from bin and once from obj), but this is typical: while everything is green, you don't care about the count. When we have introduced the first test depending on the config, we had only one failure (because the one from bin was passing), so the duplication was not outstanding.



回答2:

In addition to Gaspar Nagy's accepted answer, check to see if your project has multiple test dlls and one of them is referencing another.

This causes the referenced dll to be run twice, and the copy that was in the other dll's folder to not have the proper app.config entries. The proper fix is to remove any and all references from the other test project.



回答3:

TeamCity (v6.5.4) has its own NUnit Test Runner and there seems to be an inconsistency between it and the NUnit GUI test runner (2.5.10). The NUnit GUI Test Runner follows the long standing convention of expecting the configuration file name to be of the format .config. You can see this in NUnit by looking at Project -> Edit...

TeamCity on the other hand is looking for an app.config.

Your options are to either:

  1. Set the NUnit GUI to point to app.config and include the resultant nunit project in your source control.
  2. Have both an app.config and a .config - syncing both manualy.
  3. Add a step to your build process to copy .config to app.config (or vice versa).


回答4:

I had similar woes

This may help; additionally we had issues where this still would not work - we ended up copying the relevant config sections into the highest level config file. (i.e. if it was a web app copy it into the Web.Config) - fairly kludgy but we had wasted a few days on the issue



回答5:

I learned recently that app.config files are not read for a class library... Maybe this link could help :)

app.config for a class library



回答6:

If you need a config file for your "unit" tests then you are doing it wrong. Proper unit testing never needs configuration or access to the database, file system etc. You should change your testing strategy.

Good point to start is mark your tests that need configuration with the[Category("Integration")] annotation and set the Teamcity test runner to ignore this category. Then you should focus on refactoring these test.