35

When i run a simple test on connection to DB check i receive an error in NUnit:

[Test]
public void TestConn()
{
    string  connectionString = ConfigurationManager.ConnectionStrings["FertigungRead"].ConnectionString;
    SqlConnection connection = new SqlConnection(connectionString);
    connection.Open();
    Assert.AreEqual(ConnectionState.Open, connection.State);
    connection.Close();
 }

System.NullReferenceException : Object reference not set to an instance of an object.

on line :

connectionString = ConfigurationManager.ConnectionStrings["FertigungRead"].ConnectionString;

Can i use ConfigurationManager in tests?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Mike
  • 659
  • 2
  • 7
  • 9
  • 2
    running into the same problem. Problem is, when you use nunit.exe to run the tests (dont't know if you use that one too), it uses the app.config of nunit. Offcourse you can place the connectionstring in there, but besied being a little weird place, this (existing) file has a line in it saying 'don't use it to store your own appsettings' (which we are free to ignore, but it seems there must be another possibility) – Michel Jul 02 '10 at 10:16
  • Usually, this has to do with where your config file is in relation to the "current directory" when you run NUnit, what the config file's name is, and how you run your unit tests (e.g., open the .dll in NUnit directly, or open a project?). Can you clarify these 3 things? – jyoungdev Jul 15 '10 at 19:40
  • I got the same problem now: http://stackoverflow.com/questions/11517214/referenced-assembly-can-not-read-app-config – Hans Leautaud Jul 17 '12 at 07:04

9 Answers9

28

Yes, you can. You need to be sure that any configuration you are referencing in your tests actually exist in the app.config of the test project.

In other words, the project where your test is in, does not have a connection string "FertigungRead" defined in its app.config.

One way to do this is to add the app.config of the system under test to the test project as a link, this way any changes happen on both projects.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
6
  1. Go to NUnit/Project/Edit...
  2. In Configuration Property panel go to Configuration File Name
  3. Put there yourAssemblyName.dll.config

NB: if does not work try to add path to it, e.g. bin\Debug\ yourAssemblyName.dll.config

Your test project file yourAssemblyName.nunit will be updated.

And yes, be sure App.config in your test project match to what you access in test, i.e.

[Test]
public void TestConn()
{
   var sss = ConfigurationManager.AppSettings["TestKey"];
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="TestKey" value="testKeyValue"/>
  </appSettings>
</configuration>
ivan_d
  • 393
  • 1
  • 5
  • 12
5

I would like to add a point. There is a note in documentation of nunit, and depending upon usage scenario config file naming and placement should be done. I was stuck on this issue for a while.

Documentation says :

If a single assembly is being loaded, then the configuration file is given the name of the assembly file with the config extension. For example, the configuration file used to run nunit.tests.dll must be named nunit.tests.dll.config and located in the same directory as the dll.

If an NUnit project is being loaded, the configuration file uses the name of the project file with the extension changed to config. For example, the project AllTests.nunit would require a configuration file named AllTests.config, located in the same directory as AllTests.nunit. The same rule is followed when loading Visual Studio projects or solutions.

http://www.nunit.org/index.php?p=configFiles&r=2.2.10

amarnath chatterjee
  • 1,942
  • 16
  • 15
3

Your unit tests should still work as long as you have the same configuration for your test project as for your main project.

I'd suggest using a pre-build event in your test project to copy your application's configuration file over to the test project. This saves having to maintain two sets of configuration.

copy $(SolutionDir)path-to-main-project\Web.config $(ProjectDir)App.config

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
1

Why do you need a unit test to see if SqlConnection works? You should test your code, not Microsoft's. I don't really see the point in checking if the connection string is correct either in your unit tests. The configuration used by the unit tests isn't the same as what will be used by your production code.

In general, though, if you need some configuration data for unit tests, create an app.config file in the test project. Populate the appSettings and connectionStrings elements, etc. with appropriate values for your test environment. Don't bother testing whether ConfigurationManager or SqlConnection works, though. You'll just be creating code that you have to maintain, but that doesn't actually verify any of the production code you are writing.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • 4
    There's unit tests, and then there's integration tests. You have to test that your SQL (persistence layer) code works at some point! Also you do want to do at least some end-to-end testing. – csauve Mar 25 '13 at 17:08
  • Not to mention that NUnit is also used to run things like Selenium UI tests. Yes, it's primarily a unit test framework, but that's not all it's used for. – BardMorgan Sep 17 '15 at 15:34
  • @csauve the point is that he's only checking if the connection string works... Nothing else. – Tiago César Oliveira Jul 13 '16 at 15:23
  • 5
    It is safe to assume that the question was a generic example to make it easier to understand the important bits. – csauve Jul 13 '16 at 19:07
1

When a configuration file is used in the project in which a test is run, specific naming conventions must be followed.

The configuration file name should be the name of the assembly file with the config extension. For example, the configuration file used to run MyUnitTest.tests.dll must be named MyUnitTest.tests.dll.config and it should be in the same directory as the MyUnitTest.nunit

Also we can configure this in prebuilds like below

copy $(SolutionDir)path-to-main-project\Web.config $(ProjectDir)App.config 
javaDeveloper
  • 1,403
  • 3
  • 28
  • 42
1

See my answer nunit and configs You need to tell nunit what the name of the config file is. it looks for namespace.config by default it seams

Community
  • 1
  • 1
skyfoot
  • 20,629
  • 8
  • 49
  • 71
1

Man give a look at: http://nunit.net/blogs/?p=9

As him suggest I put a MyProjectTests.dll.config in project root and everything works.

An example of my config file is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
     <add key="TestKey" value="Ok!"/>
</appSettings>
</configuration>

And I'm using the simple: ConfigurationManager.AppSettings["TestKey"];

Custodio
  • 8,594
  • 15
  • 80
  • 115
0

I was in the same situation and I tried to add app.config to NUnit project but the project couldn't identify it. The tricky workaround for me was to create a class library that contains an app config and turn in into my test project. At the end of the day, all NUnit and its adapter are NuGet packages that can be added for tests to a class library and tests work perfectly fine ran by Visual Studio and Resharper.

enter image description here

enter image description here

enter image description here

enter image description here

And finally debugged my test and received the value from App.config file within my test project:

enter image description here

Ben
  • 953
  • 12
  • 27