How to use an NUnit Configuaration file to pass values to a test at runtime?
- Create your XML Configuration file using the format below. Add your Key,Value pairs in appSettings section
- Use the correct file name for your config file
- Add a reference to System.Configuration and add a corresponding using statement
- Use the ConfigurationManager object to access your parameters in your test code
(1) Basic Config File is in this format:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <appSettings> <add key="TestURL" value="http://www.example.com/"/> <add key="Browser" value="Firefox"/> </appSettings> </configuration>
(2) Config File Naming Scheme
Your config file has to be in the same folder as the test dll, and it must be the same name with .config suffixed. For example if you test dll is called MySeleniumTest.dll your config file must be called MySeleniumTest.dll.config, and it must be located in the same folder.
(3) Add a Reference to System.Configuration
In your Visual Studio project, add a reference to System.Configuration, as well as a corresponding using statement:
(4) Use ConfigurationManager object
Here is an example of how you can access your config parameters in your NUnit tests:
testURL = ConfigurationManager.AppSettings["TestURL"]; browser = ConfigurationManager.AppSettings["Browser"];
This post originally appeared on /bloggarriv