Skip to content

Commit 31ee35e

Browse files
Fixed error with reflection on integration test configure builder attributes, so integration tests can be created outside of the Umbraco integration test project (#19077)
* Fixed error with reflection on integration test configure builder attributes, so integration tests can be created outside of the Umbraco integration test project. * Fix nullability --------- Co-authored-by: mole <[email protected]>
1 parent cf0f3f1 commit 31ee35e

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,25 +189,48 @@ protected void ConfigureServices(IServiceCollection services)
189189

190190
private void ExecuteBuilderAttributes(IUmbracoBuilder builder)
191191
{
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.");
193194

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))
198197
{
199198
builderAttribute.Execute(builder);
200199
}
201200

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))
206203
{
207204
builderAttribute.Execute(builder);
208205
}
209206
}
210207

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+
211234
/// <summary>
212235
/// Hook for altering UmbracoBuilder setup
213236
/// </summary>

0 commit comments

Comments
 (0)