Unable to get aspect testing to work

I've got a very simple class set up to apply a logging aspect to so I can get the hang down of testing aspects. I've created the starting class to test with Metalama.Framework (latest preview) installed, viewed the preview to get the aspect-applied version and copied that into <name>.t.cs and then installed the Metalama.Testing.AspectTesting package (latest preview). When I run all tests, I get the following (per the attached image). Any guesses what I'm doing wrong here?
No description
33 Replies
Whit
WhitOP15mo ago
Using xUnit 2.5.1 and .net7.0
domsinclair
domsinclair15mo ago
I find it easier to set the test project up (and include the metalama testing nuget first (but realistically that shouldn't make too much of a difference when you add it , just as long as you do). However where you source your 'compiled' code from will make a difference. If you haven't watched it yet then I recomend looking at this video ( https://doc.metalama.net/videos/testing ). Note the recomendation to run the test first so that it fails and then use the code that the failed test message expects to see as the 'compiled' code that you add to the .t.cs file.
Lastly it might pay you to go back to xUnit 2.5. Not sure if 2.51 has been tested yet with metalama.
Petr Onderka
Petr Onderka15mo ago
I was not able to reproduce this using xunit 2.5.1-pre.26. Can you share a project that reproduces the issue? (I suspect it's something in the .csproj.)
Whit
WhitOP15mo ago
Here's my csproj:
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Metalama.Framework" Version="2023.3.4-preview" />
<PackageReference Include="Metalama.Testing.AspectTesting" Version="2023.3.4-preview" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common.Aspects.Logging\Common.Aspects.Logging.csproj" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Metalama.Framework" Version="2023.3.4-preview" />
<PackageReference Include="Metalama.Testing.AspectTesting" Version="2023.3.4-preview" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common.Aspects.Logging\Common.Aspects.Logging.csproj" />
</ItemGroup>

</Project>
Tried removing the .t.cs file and still see the same error: System.InvalidOperationException : The test assembly must have an AssemblyMetadataAttribute with Key = "ProjectDirectory". Tried updating the csproj to more accurately reflect the sample given in the docs, but same error:
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Metalama.Framework" Version="2023.3.4-preview" />
<PackageReference Include="Metalama.Testing.AspectTesting" Version="2023.3.4-preview" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common.Aspects.Logging\Common.Aspects.Logging.csproj" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Metalama.Framework" Version="2023.3.4-preview" />
<PackageReference Include="Metalama.Testing.AspectTesting" Version="2023.3.4-preview" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common.Aspects.Logging\Common.Aspects.Logging.csproj" />
</ItemGroup>

</Project>
Petr Onderka
Petr Onderka15mo ago
Do you see the same exception if you remove the ProjectReference? (And do whatever you need to make the project compile without that.)
Whit
WhitOP15mo ago
The project appears to compile without an issue - I'm just getting that error in the test runner
Whit
WhitOP15mo ago
No description
Petr Onderka
Petr Onderka15mo ago
I understand that. But I assume it won't compile if you remove the ProjectReference.
Whit
WhitOP15mo ago
Probably not since that's where the aspect lives
Whit
WhitOP15mo ago
No description
Whit
WhitOP15mo ago
I've watched about half the video about it so far and I don't see that I've done anything differently here absent updating the nuget packages from the template But here he shows the invalid test, I get this error
Petr Onderka
Petr Onderka15mo ago
I'm still unable to reproduce this. Can you share the whole solution that contains the two projects?
Whit
WhitOP15mo ago
Yeah, give me a few It's part of a much larger solution, so let me extract it out
Petr Onderka
Petr Onderka15mo ago
Thanks.
Whit
WhitOP15mo ago
What's your email? I'll invite you to the project. @petronderka Found you - added on github
Petr Onderka
Petr Onderka15mo ago
Thanks, I get the exception now and will investigate what's causing it.
Whit
WhitOP15mo ago
Awesome, thank you
Petr Onderka
Petr Onderka15mo ago
I still don't understand the problem, but it seems a workaround is to run dotnet test from the command line once. After that, it seems running the tests from VS works.
Whit
WhitOP15mo ago
Curious, but I'll give that a shot
Whit
WhitOP15mo ago
I'm still seeing the same exception at test-time in the command prompt:
No description
Petr Onderka
Petr Onderka15mo ago
Odd, that's not what I see. Anyway, I'll keep looking.
Whit
WhitOP15mo ago
I appreciate it! I wonder if the issue isn't with the fact that this aspect is making use of the dependency injection feature - as it's a standalone unit test with no entry point, there's no opportunity to register DI. At compile time, it wouldn't care - it'd set it up as a field and in the constructor as it would, but at runtime there would be an attempted injection that wouldn't actually happen. I don't know what magic the Metalama Aspect Testing package is trying to do under the hood, but perhaps it's something related to this?
Petr Onderka
Petr Onderka15mo ago
I don't think so. It seems to be caused by our MSBuild customization. If build events happen in a different order, it doesn't generate assembly attributes that are needed by the test framework.
Whit
WhitOP15mo ago
Well, I'll just disable the test project until you're able to produce another preview build with a fix then
Petr Onderka
Petr Onderka15mo ago
I have a fix for this that should be released in the 2023.3 RC release, which is planned for this week.
Whit
WhitOP15mo ago
Very exciting - thank you
Whit
WhitOP15mo ago
@petronderka I don't see anything about this in the release notes (https://github.com/orgs/postsharp/discussions/218) for 2023.3.5-rc - did it make it in?
GitHub
Metalama 2023.3.5-rc released · postsharp · Discussion #218
We've just released Metalama 2023.2.5-rc which includes all fixes and enhancements from Metalama 2023.1.12 and Metalama 2023.2.5 in addition to following changes. New You can now use the C# Dev...
Whit
WhitOP15mo ago
GitHub
Metalama 2023.4.1-preview released · postsharp · Discussion #219
We've just released Metalama 2023.4.1-rc which includes all fixes and enhancements from Metalama 2023.1.12, Metalama 2023.2.5, and 2023.3.5-rc, in addition to following changes. Fixed Fixed: Te...
Gael Fraiteur
Gael Fraiteur15mo ago
Yes it was released.
Whit
WhitOP15mo ago
Excellent, thank you
Gael Fraiteur
Gael Fraiteur15mo ago
I see we didn't solve tickets after the release but in this case there is no ticket.
Whit
WhitOP15mo ago
Yes, that's right. Once he was able to repro it himself, I didn't see a need to file a separate issue in Github as well
Gael Fraiteur
Gael Fraiteur15mo ago
Yes because Discord/Slack are not integrated with our bug tracking system. GitHub is.
Want results from more Discord servers?
Add your server