I have set up a .nunit file to specify a local and remote test suite App.config. Here is my code :
<NUnitProject>
<Settings activeconfig="local"/>
<Config name="local" configfile="App.config">
<assembly path="bin\Debug\Proj.dll"/>
</Config>
<Config name="remote" configfile="App.Remote.config">
<assembly path="bin\Debug\Proj.dll"/>
</Config>
</NUnitProject>
Now, when I run the following command in my command line
"nunit3-console" "test_runner.nunit" /config:remote
It is still running off of the App.config and not the App.Remote.config file I specified in the Config block.. Am I missing something here? Do I need to have some sort of referance in my App.config?
I missed the reply to my comment! In case you haven't already done so, you should ensure that the config file is copied to the output directory.
Related
I have a test project which uses MSTest. And I have a testsettings file and have a properties in that file. as below.
<?xml version="1.0" encoding="UTF-8"?>
<TestSettings name="local" id="77572268-dd99-4f8c-a660-f5c8c1eec977"
xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<Description>These are default test settings for a local test run.</Description>
<Execution>
<TestTypeSpecific>
<UnitTestRunConfig testTypeId="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b">
<AssemblyResolution>
<TestDirectory useLoadContext="true" />
</AssemblyResolution>
</UnitTestRunConfig>
</TestTypeSpecific>
<AgentRule name="Execution Agents">
</AgentRule>
</Execution>
<Properties >
<Property name="AAA" value="val1"></Property>
<Property name="BBB" value="val2"></Property>
</Properties>
</TestSettings>
But how can I access these properties in testsettings file values by name in runtime. How can I do that?
This is what currently I'am trying..
[ClassInitialize]
public static void TestClassInitialize(TestContext context)
{
var sad = context.Properties["AAA"].ToString();
}
And it gives following exception
An exception of type 'System.NullReferenceException' occurred in
TestAutomation.dll but was not handled in user code
Additional information: Object reference not set to an instance of an
object.
And this is not about the System.NullReferenceException and this is about how to access a property in a Test settings file in runtime. So this question is not a duplicate.
I suspect you have not properly configured your .runsettings file.
Follow this link .runsettings file configuration and configure the "SettingsFile" section properly.
Alternatively you can also try "TestRunParameters" section to get this working.
The way you are accessing the properties is not correct. You need to use runsettings file instead.
You were close but you dont need the ending tag of the property tag, here is an example:
<Properties>
<Property name="test" value="testValue"/>
</Properties>
Once that is done you can use the same code to access the data:
context.Properties["AAA"].ToString()
I have about 10 methods in my class. In every method I use ConfigurationManager.AppSettings to get value form App.config file
like
_applicationPort = int.Parse(ConfigurationManager.AppSettings["ApplicationPort"]
My problem is that I want to make this code get AppSettings from another app.config file like AnotherPoject.exe.config.
You can also set the app.config to read another file. Something like this:
<?xml version="1.0"?>
<configuration>
<appSettings file="my\custom\file\path\external.config"/>
</configuration>
and the external.config will have the appSettings section:
<appSettings>
<add key="myKey" value="myValue" />
</appSettings>
refer to this msdn for additional info.
You could do something like this
var fileConfig = ConfigurationManager.OpenExeConfiguration("<filePath>");
int port = int.Parse(fileConfig.AppSettings["PortNumber"].ToString());
You can accomplish this by using ConfigurationManager.OpenExeConfiguration. This will allow you to open another configuration file easily.
MSDN article about OpenExeConfiguration.
I created a second web config and placed it in a folder:
~/Configuration/OtherConnections.config
My config file looks like:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="serverurl" value="http://serverUrl" />
<add key="UserName" value="myUser" />
<add key="Password" value="XXXXXXX" />
</appSettings>
</configuration>
When I attempt to read the value from one of the items like:
string connectionInfo = ConfigurationManager.AppSettings["UserName"];
I do not get a value back. Is this because the web config is in a folder, or is there something else going on in this web app?
I do not get a value back. Is this because the web config is in a folder ... ?
No, not the folder but the filename. You can use ~/Configuration/Web.config but then you have to explicity open it:
var config = WebConfigurationManager.OpenWebConfiguration("~/Configuration");
And then to read from it:
string url = config.AppSettings.Settings["serverurl"].Value;
Note that you cannot specify (and thus not change) the actual web.config file name. Just the folder.
you can have only one web.config file for each web folder
There are tow options anyway:
In the IIS Manager you need to configure the sub folder as a new application. It uses the web.config file from the running app.
Another option is using a single config file and adding a <location> section to segment the file to act differently for some folders or files. (which I would suggest more info here)
You can access multiple config files by using WebConfigurationmanager method. add namespace:
using System.Web.Configuration;
So, to access the appSettings of
../SomeProjectFolder/Environment/Web.config, you can do:
var config = WebConfigurationManager.OpenWebConfiguration("~/SomeProjectFolder/Environment/");
string username = config.AppSettings.Settings["username"].Value;
Hope this helps.
I am developing a plugin for a .NET 4 application and I want to add a config file to the dll as I dont want to put the configuration in the main config file.
I have added the app.config t the project and it is correctly compile and dllName.dll.config generated.
Here is my configuration file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MyTabsConfig" type="NewApp.UI.MyTabsConfigHandler, NewApp.UI" />
</configSections>
<MyTabsConfig>
<MyTabs>
<MyTab Name="First" Leads="2" />
<MyTab Name="Second" Leads="4" />
<MyTab Name="Third" Leads="1" />
</MyTabs>
</MyTabsConfig>
</configuration>
Now I have 1 problems:
If I copy the file in the ExtraPlugins directory of my main application, NewApp.UI.dll cannot be found when calling GetSection("MyTabsConfig"). I think it is looking in the main application folder.
Thanks.
Have you tried something like this?
ConfigurationSection section = ConfigurationManager.OpenExeConfiguration("myConfig.config").GetSection("mySection");
I have a config file that is used in several projects, general.config, looks like:
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="mykey1" value="myvalue1"/>
<add key="mykey2" value="myvalue2"/>
</appSettings>
In one of the projects, I need to override one of the two settings. So the app.config of this project looks like:
<?xml version="1.0"?>
<configuration>
<appSettings file="general.config">
<remove key="mykey1"/>
<add key="mykey1" value="anothervalue"/>
<add key="mykey3" value="myvalue3"/>
</appSettings>
</configuration>
But remove is not working here. How can I override mykey1 without breaking mykey2? add works in this case. I can get myvalue3 from ConfigurationManager.
EDIT: general.config is copied to output folder automatically when compiling. Don't worry about the path issue. Currently I got:
ConfigurationManager.AppSettings["mykey1"]
//I got "myvalue1", but I want "anothervalue" here
//that is, this item is "overrided", just like virtual methods in C#
ConfigurationManager.AppSettings["mykey2"]
//this setting will not be modified, currently it works fine
ConfigurationManager.AppSettings["mykey3"] //good
A friend of mine answered this question. From MSDN:
You can use the file attribute to
specify a configuration file that
provides additional settings or
overrides the settings that are
specified in the appSettings element.
You can use the file attribute in
source control team development
scenarios, such as when a user wants
to override the project settings that
are specified in an application
configuration file. Configuration
files that are specified in a file
attribute must have the appSettings
element rather than configuration
element as the root node.
So in this question, the settings in general.config overrides items in app.config, different from that I think(want) app.config items overrides items in general.config. Now I think I have to resolve this issue in C# code(it will inevitable looks ugly).
Your use of the file attribute to load common settings with an expectation that keys added directly to the <appSettings> element would override those common settings is understandable, but unfortunately that is not how it works.
Microsoft's intention was for the file attribute to load common settings that override the individual application's settings.
This is discussed in some detail in the Microsoft Documentation
To overcome this problem, we very occasionally declare base settings in the common file, and then appropriately named overrides in the application configuration. However this does require additional code which is a bit ugly. e.g.
var config = ConfigurationManager.AppSettings["MSG_QUEUE_PROVIDER_OVERRIDE"]
?? ConfigurationManager.AppSettings["MSG_QUEUE_PROVIDER"]
?? "ActiveMQ";
<appSettings file="common.config">
<!-- Override the common values -->
<add key="MSG_QUEUE_PROVIDER_OVERRIDE" value="RabbitMQ"/>
</appSettings>
The elements are changed from the child and what i mean by that is currently your app.config is the parent file and the values are replaced by the ones existing in General.config
Since you are using remove in the parent file what its effectively doing is removing the element you specify in app.config but after that the elements from general.config are pushed in. Now say here in General.config you say remove mykey3 which is on your app.config you will see that the final collection has no key as mykey3.
In short this is not going to work. Hope this helped you.
You can add another config file say Test.config.
<appSettings>
<add key="mykey1" value="New value"/>
</appSettings>
and in the app.config appsettings section will look like this
<appSettings file="Test.config">
<add key="mykey1" value="myvalue1"/>
</appSettings>