在powershell中检索NUnit自定义属性
假设您有一个包含NUnit测试的.dll文件,其中所有测试过程都具有在powershell中检索NUnit自定义属性,powershell,attributes,nunit,Powershell,Attributes,Nunit,假设您有一个包含NUnit测试的.dll文件,其中所有测试过程都具有[test]属性。 有些测试是有文档记录的,这意味着它们被标记为[测试,描述(…)] 我想我可以使用PowerShell中的反射检索所有具有[Test]属性和相关描述(如果有的话)的函数 我试图查看是否可以在以下内容中看到上述属性: $suite = [Reflection.Assembly]::ReflectionOnlyLoadFrom("D:\test_suite.dll") [reflection.customattri
[test]
属性。
有些测试是有文档记录的,这意味着它们被标记为[测试,描述(…)]
我想我可以使用PowerShell中的反射检索所有具有[Test]
属性和相关描述(如果有的话)的函数
我试图查看是否可以在以下内容中看到上述属性:
$suite = [Reflection.Assembly]::ReflectionOnlyLoadFrom("D:\test_suite.dll")
[reflection.customattributedata]::GetCustomAttributes($suite)
但我所看到的没有测试的痕迹:
AttributeType Constructor ConstructorArguments NamedArguments
------------- ----------- -------------------- --------------
System.Runtime.InteropSer... Void .ctor(System.String) {"02cfdc16-5480-4bb6-890... {}
System.Diagnostics.Debugg... Void .ctor(DebuggingModes) {(System.Diagnostics.Deb... {}
System.Reflection.Assembl... Void .ctor(System.String) {"Copyright © Microsoft ... {}
System.Reflection.Assembl... Void .ctor(System.String) {"1.0.0.0"} {}
System.Runtime.InteropSer... Void .ctor(Boolean) {(Boolean)False} {}
System.Runtime.CompilerSe... Void .ctor(Int32) {(Int32)8} {}
System.Runtime.CompilerSe... Void .ctor() {} {WrapNonExceptionThrows ...
System.Reflection.Assembl... Void .ctor(System.String) {""} {}
System.Reflection.Assembl... Void .ctor(System.String) {"Test_Test_Suite"} {}
System.Reflection.Assembl... Void .ctor(System.String) {""} {}
System.Reflection.Assembl... Void .ctor(System.String) {""} {}
System.Reflection.Assembl... Void .ctor(System.String) {"Microsoft"} {}
System.Reflection.Assembl... Void .ctor(System.String) {"Test_Test_Suite"} {}
我显然走错了路。有什么提示吗?问题是您正在查找程序集的属性,而不是程序集中的方法 要做到这一点,只需获取程序集中的类型,然后获取它们的方法,然后过滤这些类型,以仅获取具有TestAttribute而非DescriptionAttribute的方法 因此,首先,只需像您已经做的那样获取程序集元数据:
$testSuite = [System.Reflection.Assembly]::ReflectionOnlyLoadFrom("c:\testsuite.dll")
然后获取程序集中可用的方法:
$methods = $testSuite.GetTypes().GetMethods()
当您拥有这些属性时,过滤掉具有TestAttribute的方法。您可能可以用比这更好的方式确定TestAttribute的存在,例如给出您要查找的确切属性名称或类似名称,但我在自己的示例中没有使用NUnit属性
$testMethods = $methods |
Where {
$_.GetCustomAttributesData() |
Where { $_.AttributeType.FullName -like "*.TestAttribute" }
}
完成后,只需从中选择所需的数据(我还包括DeclaringType,如果您不想在选择中删除它,只需将其删除):
或者,如果你愿意的话,你可以在一行中完成这一切:
[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
}
}
对于我的小测试程序集,这将产生以下输出:
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
太棒了。谢谢你详尽而清晰的回答。不幸的是,我被困在
GetTypes()
,这会引发此异常:异常调用带有“0”参数的“GetTypes”:“无法加载一个或多个请求的类型。有关详细信息,请检索LoaderExceptions属性。”
。奇怪的是,如果我捕捉到异常并查看$\.exception.InnerException.LoaderException
,那里什么都没有。错误消息似乎以复数形式声明属性名称,而属性访问器似乎是单数形式,也就是说,似乎应该是loadereException
而不是loadereException
。我建议在调试器中运行它,并在catch语句中设置断点。当我不确定我得到了什么异常,也不确定这些异常具有什么属性时,它通常会帮助我。在那里,您可以轻松地对每个异常
和每个内部异常
执行格式列表
和获取成员
。新手犯了错误,您就搞定了LoaderExceptions
包含以下内容:无法解析对程序集“My_Integration_Test_Framework,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”的依赖关系,因为它尚未预加载。使用ReflectionOnly API时,必须通过ReflectionOnlyAssemblyResolve事件预加载或按需加载依赖程序集。
目前,我通过使用LoadFrom
而不是ReflectionOnlyLoadFrom
修复了它。我以后会更深入地讨论这个问题最后,谢谢你的回答:它就像一个符咒!
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