@@ -48,7 +48,8 @@ public static class TestApplication
4848 private static bool initialiazed ;
4949
5050 private static string testAssemblyName ;
51- private static TestConfiguration testConfiguration ;
51+ private static IConfigurationBuilder configurationBuilder ;
52+ private static TestConfiguration configuration ;
5253
5354 private static IHostingEnvironment environment ;
5455
@@ -72,8 +73,7 @@ static TestApplication()
7273#if NET451
7374 FindTestAssembly ( ) ;
7475#endif
75-
76- PrepareTestConfiguration ( ) ;
76+
7777 FindTestAssemblyName ( ) ;
7878 }
7979
@@ -115,7 +115,7 @@ internal static Type StartupType
115115
116116 set
117117 {
118- if ( startupType != null && TestConfiguration . General . AsynchronousTests )
118+ if ( startupType != null && Configuration ( ) . General ( ) . AsynchronousTests ( ) )
119119 {
120120 throw new InvalidOperationException ( "Multiple Startup types per test project while running asynchronous tests is not supported. Either set 'General.AsynchronousTests' in the 'testconfig.json' file to 'false' or separate your tests into different test projects. The latter is recommended. If you choose the first option, you may need to disable asynchronous testing in your preferred test runner too." ) ;
121121 }
@@ -163,31 +163,38 @@ internal static IHostingEnvironment Environment
163163 }
164164 }
165165
166- public static TestConfiguration TestConfiguration
166+ internal static string ApplicationName =>
167+ Configuration ( ) . General ( ) . ApplicationName ( )
168+ ?? TestAssembly ? . GetName ( ) . Name
169+ ?? StartupAssemblyName
170+ ?? PlatformServices . Default . Application . ApplicationName ;
171+
172+ public static TestConfiguration Configuration ( )
167173 {
168- get
174+ if ( configuration == null || AdditionalConfiguration != null )
169175 {
170- if ( testConfiguration == null || AdditionalConfiguration ! = null )
176+ if ( configurationBuilder = = null )
171177 {
172- testConfiguration = TestConfiguration . With ( PrepareTestConfiguration ( ) ) ;
173- PrepareLicensing ( ) ;
178+ configurationBuilder = new ConfigurationBuilder ( )
179+ . AddJsonFile ( "testconfig.json" , optional : true ) ;
174180 }
175181
176- return testConfiguration ;
182+ AdditionalConfiguration ? . Invoke ( configurationBuilder ) ;
183+ AdditionalConfiguration = null ;
184+
185+ configuration = TestConfiguration . With ( configurationBuilder . Build ( ) ) ;
186+
187+ PrepareLicensing ( ) ;
177188 }
178- }
179189
180- internal static string ApplicationName =>
181- TestConfiguration . General . ApplicationName
182- ?? TestAssembly ? . GetName ( ) . Name
183- ?? StartupAssemblyName
184- ?? PlatformServices . Default . Application . ApplicationName ;
190+ return configuration ;
191+ }
185192
186193 public static void TryInitialize ( )
187194 {
188195 lock ( Sync )
189196 {
190- if ( ! initialiazed && TestConfiguration . General . AutomaticStartup )
197+ if ( ! initialiazed && Configuration ( ) . General ( ) . AutomaticStartup ( ) )
191198 {
192199 var defaultStartupType = TryFindDefaultStartupType ( ) ;
193200
@@ -267,7 +274,7 @@ internal static Type TryFindDefaultStartupType()
267274 {
268275 var applicationAssembly = TestAssembly ?? Assembly . Load ( new AssemblyName ( testAssemblyName ) ) ;
269276
270- var defaultStartupType = TestConfiguration . General . StartupType ?? $ "{ Environment . EnvironmentName } Startup";
277+ var defaultStartupType = Configuration ( ) . General ( ) . StartupType ( ) ?? $ "{ Environment . EnvironmentName } Startup";
271278
272279 // check root of the test project
273280 var startup =
@@ -292,21 +299,10 @@ private static void Initialize()
292299
293300 initialiazed = true ;
294301 }
295-
296- private static IConfiguration PrepareTestConfiguration ( )
297- {
298- var configurationBuilder = new ConfigurationBuilder ( )
299- . AddJsonFile ( "testconfig.json" , optional : true ) ;
300-
301- AdditionalConfiguration ? . Invoke ( configurationBuilder ) ;
302- AdditionalConfiguration = null ;
303-
304- return configurationBuilder . Build ( ) ;
305- }
306-
302+
307303 private static void FindTestAssemblyName ( )
308304 {
309- testAssemblyName = TestConfiguration . General . TestAssemblyName
305+ testAssemblyName = Configuration ( ) . General ( ) . TestAssemblyName ( )
310306 ?? TestAssembly ? . GetName ( ) . Name
311307 ?? DependencyContext
312308 . Default
@@ -318,7 +314,7 @@ private static void FindTestAssemblyName()
318314 private static void PrepareLicensing ( )
319315 {
320316 TestCounter . SetLicenseData (
321- TestConfiguration . Licenses ,
317+ Configuration ( ) . Licenses ( ) ,
322318 DateTime . ParseExact ( ReleaseDate , "yyyy-MM-dd" , CultureInfo . InvariantCulture ) ,
323319 TestAssembly ? . GetName ( ) . Name ?? StartupAssemblyName ) ;
324320 }
@@ -328,7 +324,7 @@ private static IHostingEnvironment PrepareEnvironment()
328324 return new HostingEnvironment
329325 {
330326 ApplicationName = ApplicationName ,
331- EnvironmentName = TestConfiguration . General . EnvironmentName ,
327+ EnvironmentName = Configuration ( ) . General ( ) . EnvironmentName ( ) ,
332328 ContentRootPath = PlatformServices . Default . Application . ApplicationBasePath
333329 } ;
334330 }
@@ -515,6 +511,8 @@ private static void TryLockedInitialization()
515511 private static void Reset ( )
516512 {
517513 initialiazed = false ;
514+ configurationBuilder = null ;
515+ configuration = null ;
518516 environment = null ;
519517 startupType = null ;
520518 serviceProvider = null ;
@@ -531,29 +529,29 @@ private static void Reset()
531529 RoutingServiceRegistrationPlugins . Clear ( ) ;
532530 InitializationPlugins . Clear ( ) ;
533531 LicenseValidator . ClearLicenseDetails ( ) ;
534- }
532+ }
535533
536534#if NET451
537- private static void FindTestAssembly ( )
538- {
539- var executingAssembly = Assembly . GetExecutingAssembly ( ) ;
540-
541- var stackTrace = new StackTrace ( false ) ;
542-
543- foreach ( var frame in stackTrace . GetFrames ( ) )
535+ private static void FindTestAssembly ( )
544536 {
545- var method = frame . GetMethod ( ) ;
546- var methodAssembly = method ? . DeclaringType ? . Assembly ;
537+ var executingAssembly = Assembly . GetExecutingAssembly ( ) ;
538+
539+ var stackTrace = new StackTrace ( false ) ;
547540
548- if ( methodAssembly != null
549- && methodAssembly != executingAssembly
550- && ! methodAssembly . FullName . StartsWith ( TestFrameworkName ) )
541+ foreach ( var frame in stackTrace . GetFrames ( ) )
551542 {
552- TestAssembly = methodAssembly ;
553- return ;
543+ var method = frame . GetMethod ( ) ;
544+ var methodAssembly = method ? . DeclaringType ? . Assembly ;
545+
546+ if ( methodAssembly != null
547+ && methodAssembly != executingAssembly
548+ && ! methodAssembly . FullName . StartsWith ( TestFrameworkName ) )
549+ {
550+ TestAssembly = methodAssembly ;
551+ return ;
552+ }
554553 }
555554 }
556- }
557555#endif
558- }
556+ }
559557}
0 commit comments