Skip to content

External management (config file)- Unable to feed connection string from config file. NUnit Console error / TC error. #716

@Lewandinh0

Description

@Lewandinh0

Hi All.
(Sorry for typing such long epos)

I was trying to configure test suite to feed ConnectionString from config file. Everything ended up working on NUnit GUI but failed when running from NUnit console and on TC build (probably also using console).

Documentation that I based upon:
http://www.nbi.io/docs/connection-configuration/

Issues that I needed to find walkaround for:

By using default configuration as per below:

<connection-strings>
    <clear />
    <add name="def-sut"
      connection-string="..." />
    <add name="ref-one"
      connection-string="..." />
    <add name="ref-two"
      connection-string="..." />
  </connection-strings>

I got following error:

************** Exception Text **************
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section connection-strings

-- for that reason I added it to the configSections in .config file as per below:

<section name="connection-strings" type="NBi.NUnit.Runtime.NBiSection, NBi.NUnit.Runtime"/>

By adding it to configSections it started to be recognised to the NUnit GUI. but I received other error

NBi.NUnit.Runtime.TestSuite.ExecuteTestCases: System.ArgumentOutOfRangeException : Specified argument was out of the range of valid values. Parameter name: Some connection-strings are provided through the config file but the default connection string is trying to reference a connection string named '@def-sut' which has not been found.

This error is bane of my existance (for the record ;) )

I have been trying different setups and noticed that it also worked on GUI upon not adding configSections but defining connections as per .NET documentation as per below format: (using: connectionStrings)

<connectionStrings>
		<clear />
		<add name="def-sut"
            connectionString="Data Source=XXX;Initial Catalog=YYY;Trusted_Connection=yes" />
		<add name="ref-one"
            connectionString="Data Source=XXX;Initial Catalog=YYY;Trusted_Connection=yes" />
		<add name="ref-two"
            connectionString="Data Source=XXX;Initial Catalog=YYY;Trusted_Connection=yes" />
	</connectionStrings>

This solved issues on NUnit GUI. Since now on the SQL Server instance that I have tried to reach was being picked up and I was able to run whole test and it passed.

After that I merged my changes to local branch and run it via TC Buld with the same error as last time, that is:

NBi.NUnit.Runtime.TestSuite.ExecuteTestCases: System.ArgumentOutOfRangeException : Specified argument was out of the range of valid values. Parameter name: Some connection-strings are provided through the config file but the default connection string is trying to reference a connection string named '@def-sut' which has not been found.

I then investigated the error using NUnit console with the same error message. (on GUI everything is working fine still). Problem is that I need for it to be running on TC build for test purposes.

Whole solution - with all the code - it should be working with no issues on GUI

.Nbits part
         

<?xml version="1.0" encoding="utf-8" ?>
<testSuite name="MyTestSuite"
	xmlns="http://NBi/TestSuite">
	<settings>
		<!--change settings according to your need-->
		<default apply-to="system-under-test">
			<connection-string>@def-sut</connection-string>
		</default>
		<!--<default apply-to="system-under-test"><connectionString>Data Source=XXX;Initial Catalog=YYY;Trusted_Connection=yes</connectionString></default>-->
		<default apply-to="assert">
			<connectionString>Data Source=XXX;Initial Catalog=YYY;Trusted_Connection=yes</connectionString>
		</default>
	</settings>
	<!--define your tests here under-->
	<test name="My first test">
		<system-under-test>
			<execution>
				<query>select 'ok'</query>
			</execution>
		</system-under-test>
		<assert>
			<equalTo>
				<column index="0" role="key" type="text"/>
				<query>
          SELECT 'ok'</query>
			</equalTo>
		</assert>
	</test>
</testSuite>

Config file part:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="nbi" type="NBi.NUnit.Runtime.NBiSection, NBi.NUnit.Runtime"/>
		<section name="connection-strings" type="NBi.NUnit.Runtime.NBiSection, NBi.NUnit.Runtime"/>
	</configSections>
	<nbi testSuite="TestSuite.nbits"/>
	<connection-strings>
		<clear />
		<add name="def-sut"
            connection-string="Data Source=XXX;Initial Catalog=YYY;Trusted_Connection=yes" />
		<add name="ref-one"
            connection-string="Data Source=XXX;Initial Catalog=YYY;Trusted_Connection=yes" />
		<add name="ref-two"
            connection-string="Data Source=XXX;Initial Catalog=YYY;Trusted_Connection=yes" />
	</connection-strings>
	<connectionStrings>
		<clear />
		<add name="def-sut"
            connectionString="Data Source=XXX;Initial Catalog=YYY;Trusted_Connection=yes" />
		<add name="ref-one"
            connectionString="Data Source=XXX;Initial Catalog=YYY;Trusted_Connection=yes" />
		<add name="ref-two"
            connectionString="Data Source=XXX;Initial Catalog=YYY;Trusted_Connection=yes" />
	</connectionStrings>
</configuration>

Error Message from NUnit Console:

System.ArgumentOutOfRangeException : Specified argument was out of the range of valid values.\nParameter name: No connection-string is provided through the config file. The default connection string stipulated in the nbits file is trying to reference a connection string named '@def-sut'

Versioning:

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/></startup>

Project:

<NUnitProject>
  <Settings activeconfig="Default" processModel="Default" domainUsage="Default" />
  <Config name="Default" binpathtype="Auto" appbase="." configfile="LOCALCONFIG.config">
    <assembly path="NBi.NUnit.Runtime.dll" />
  </Config>
</NUnitProject>

Hope that above feels readable.
Cheers,

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions