The problem is that you are looking for attributes on the assembly, not on the methods in the assembly.
To do that, just get the types in the assembly and then their methods and then filter those to get only the methods which have the TestAttribute and not the DescriptionAttribute.
So, to start with, just get the assembly metadata as you were doing already:
$testSuite = [System.Reflection.Assembly]::ReflectionOnlyLoadFrom("c:\testsuite.dll")
Then get the methods available in the assembly:
$methods = $testSuite.GetTypes().GetMethods()
When you have those, filter out the methods which have the TestAttribute. You could probably determine the existance of TestAttribute in a better way than this, for example giving the exact attribute name you are looking for or similar, but I didn't use the NUnit attributes in my own sample.
$testMethods = $methods |
Where {
$_.GetCustomAttributesData() |
Where { $_.AttributeType.FullName -like "*.TestAttribute" }
}
And when you have done so, just pick the data you want from this (I included the DeclaringType as well, just remove it if you don't want it in your selection):
$testDocumentation = $testMethods |
Select DeclaringType, Name, @{
Name = "Description"
Expression = {
$descriptionAttribute = $_.GetCustomAttributesData() |
Where { $_.AttributeType.FullName -like "*.DescriptionAttribute"}
Write-Output $descriptionAttribute.ConstructorArguments[0].Value
}
}
Or you could do it all in one line, if you are so inclined:
[Reflection.Assembly]::ReflectionOnlyLoadFrom("$pwd\ClassLibrary2.dll").GetTypes().GetMethods() |
Where { $_.GetCustomAttributesData() | Where { $_.AttributeType.FullName -like "*.TestAttribute" } } |
Select DeclaringType, Name, @{
Name = "Description"
Expression = {
$descriptionAttribute = $_.GetCustomAttributesData() |
Where { $_.AttributeType.FullName -like "*.DescriptionAttribute"}
Write-Output $descriptionAttribute.ConstructorArguments[0].Value
}
}
With my little test assembly, this resulted in the following output:
DeclaringType Name Description
------------- ---- -----------
TestSuite.TestClass1 FirstTest This is the first test
TestSuite.TestClass1 SecondTest This is my second test
TestSuite.TestClass1 ThirdTest This is my third test