@@ -189,25 +189,48 @@ protected void ConfigureServices(IServiceCollection services)
189
189
190
190
private void ExecuteBuilderAttributes ( IUmbracoBuilder builder )
191
191
{
192
- // todo better errors
192
+ Type ? testClassType = GetTestClassType ( )
193
+ ?? throw new Exception ( $ "Could not find test class for { TestContext . CurrentContext . Test . FullName } in order to execute builder attributes.") ;
193
194
194
- // execute builder attributes defined on method
195
- foreach ( ConfigureBuilderAttribute builderAttribute in Type . GetType ( TestContext . CurrentContext . Test . ClassName )
196
- . GetMethods ( ) . First ( m => m . Name == TestContext . CurrentContext . Test . MethodName )
197
- . GetCustomAttributes ( typeof ( ConfigureBuilderAttribute ) , true ) )
195
+ // Execute builder attributes defined on method.
196
+ foreach ( ConfigureBuilderAttribute builderAttribute in GetConfigureBuilderAttributes < ConfigureBuilderAttribute > ( testClassType ) )
198
197
{
199
198
builderAttribute . Execute ( builder ) ;
200
199
}
201
200
202
- // execute builder attributes defined on method with param value passtrough from testcase
203
- foreach ( ConfigureBuilderTestCaseAttribute builderAttribute in Type . GetType ( TestContext . CurrentContext . Test . ClassName )
204
- . GetMethods ( ) . First ( m => m . Name == TestContext . CurrentContext . Test . MethodName )
205
- . GetCustomAttributes ( typeof ( ConfigureBuilderTestCaseAttribute ) , true ) )
201
+ // Execute builder attributes defined on method with param value pass through from test case.
202
+ foreach ( ConfigureBuilderTestCaseAttribute builderAttribute in GetConfigureBuilderAttributes < ConfigureBuilderTestCaseAttribute > ( testClassType ) )
206
203
{
207
204
builderAttribute . Execute ( builder ) ;
208
205
}
209
206
}
210
207
208
+ private static Type ? GetTestClassType ( )
209
+ {
210
+ string testClassName = TestContext . CurrentContext . Test . ClassName ;
211
+
212
+ // Try resolving the type name directly (which will work for tests in this assembly).
213
+ Type testClass = Type . GetType ( testClassName ) ;
214
+ if ( testClass is not null )
215
+ {
216
+ return testClass ;
217
+ }
218
+
219
+ // Try scanning the loaded assemblies to see if we can find the class by full name. This will be necessary
220
+ // for integration test projects using the base classess provided by Umbraco.
221
+ var assemblies = AppDomain . CurrentDomain . GetAssemblies ( ) ;
222
+ return assemblies
223
+ . SelectMany ( a => a . GetTypes ( ) . Where ( t => t . FullName == testClassName ) )
224
+ . FirstOrDefault ( ) ;
225
+ }
226
+
227
+ private static IEnumerable < TAttribute > GetConfigureBuilderAttributes < TAttribute > ( Type testClassType )
228
+ where TAttribute : Attribute =>
229
+ testClassType
230
+ . GetMethods ( ) . First ( m => m . Name == TestContext . CurrentContext . Test . MethodName )
231
+ . GetCustomAttributes ( typeof ( TAttribute ) , true )
232
+ . Cast < TAttribute > ( ) ;
233
+
211
234
/// <summary>
212
235
/// Hook for altering UmbracoBuilder setup
213
236
/// </summary>
0 commit comments