diff --git a/tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs b/tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs index 585f6b268c..ca5654eadd 100644 --- a/tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs +++ b/tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs @@ -189,25 +189,48 @@ public abstract class UmbracoIntegrationTest : UmbracoIntegrationTestBase private void ExecuteBuilderAttributes(IUmbracoBuilder builder) { - // todo better errors + Type? testClassType = GetTestClassType() + ?? throw new Exception($"Could not find test class for {TestContext.CurrentContext.Test.FullName} in order to execute builder attributes."); - // execute builder attributes defined on method - foreach (ConfigureBuilderAttribute builderAttribute in Type.GetType(TestContext.CurrentContext.Test.ClassName) - .GetMethods().First(m => m.Name == TestContext.CurrentContext.Test.MethodName) - .GetCustomAttributes(typeof(ConfigureBuilderAttribute), true)) + // Execute builder attributes defined on method. + foreach (ConfigureBuilderAttribute builderAttribute in GetConfigureBuilderAttributes(testClassType)) { builderAttribute.Execute(builder); } - // execute builder attributes defined on method with param value passtrough from testcase - foreach (ConfigureBuilderTestCaseAttribute builderAttribute in Type.GetType(TestContext.CurrentContext.Test.ClassName) - .GetMethods().First(m => m.Name == TestContext.CurrentContext.Test.MethodName) - .GetCustomAttributes(typeof(ConfigureBuilderTestCaseAttribute), true)) + // Execute builder attributes defined on method with param value pass through from test case. + foreach (ConfigureBuilderTestCaseAttribute builderAttribute in GetConfigureBuilderAttributes(testClassType)) { builderAttribute.Execute(builder); } } + private static Type? GetTestClassType() + { + string testClassName = TestContext.CurrentContext.Test.ClassName; + + // Try resolving the type name directly (which will work for tests in this assembly). + Type testClass = Type.GetType(testClassName); + if (testClass is not null) + { + return testClass; + } + + // Try scanning the loaded assemblies to see if we can find the class by full name. This will be necessary + // for integration test projects using the base classess provided by Umbraco. + var assemblies = AppDomain.CurrentDomain.GetAssemblies(); + return assemblies + .SelectMany(a => a.GetTypes().Where(t => t.FullName == testClassName)) + .FirstOrDefault(); + } + + private static IEnumerable GetConfigureBuilderAttributes(Type testClassType) + where TAttribute : Attribute => + testClassType + .GetMethods().First(m => m.Name == TestContext.CurrentContext.Test.MethodName) + .GetCustomAttributes(typeof(TAttribute), true) + .Cast(); + /// /// Hook for altering UmbracoBuilder setup ///