问题
I'm getting error C3095: 'Xunit::Extensions::InlineDataAttribute': attribute cannot be repeated
in C++/CLI code but not C#.
xUnit.net looks like the answer to my prayers - a modern unit test framework with GUI working with C++/CLI. However, using their approach to parameterised testing gives me the error C3095 as shown below.
Any ideas?
I'm using the latest xUnit.net 1.6 with Visual Studio 2008SP1.
using namespace Xunit;
using namespace Xunit::Extensions;
public ref class ParameterisedTestClass
{
public:
[Theory]
[InlineData("Kilroy", 6)]
// uncomment to cause c3095 [InlineData("Jones", 5)]
void PropTest(String^ msg, int msgLen)
{
Assert::Equal(msg->Length, msgLen);
}
};
the equivalent in C# is fine
using Xunit;
using Xunit.Extensions;
public class ParameterisedTestClass
{
[Theory]
[InlineData("Kilroy", 6)]
[InlineData("Jones", 5)]
public void PropTest(String msg, int msgLen)
{
Assert.Equal(msg.Length, msgLen);
}
};
回答1:
Hmmm... I looked at the defs here and here, and reproduced them (cut-down) below; the inheritance of AllowMultiple
via DataAttribute
works fine in C#:
class Test
{
[InlineData]
[InlineData]
static void Main() { }
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
class DataAttribute : Attribute {}
class InlineDataAttribute : DataAttribute { }
So if it isn't working for C++/CLI, I guess C++/CLI simply isn't processing the implied [AttributeUsage]
. You should file a request against Xunit asking them to make the [AttributeUsage]
explicit on InlineDataAttribute
.
回答2:
My guess is that this is due to inheritance, and one of the compilers getting it wrong.
InlineDataAttribute inherits from DataAttribute. Now DataAttribute
is declared to allow multiple instances:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
but InlineDataAttribute
doesn't have any explicit AttributeUsage
attribute itself. I suspect the C++ compiler isn't spotting the inherited AllowMultiple... or it's possible that it shouldn't be inherited. I can't find any detailed documentation about the inheritance of AttributeUsageAttribute itself - although it has Inherited=true
, so I guess it should be inherited completely by InlineDataAttribute
...
回答3:
I repro with this C++/CLI snippet:
[AttributeUsage(AttributeTargets::All, AllowMultiple = true, Inherited = true)]
ref class BaseAttribute : Attribute {};
ref class DerivedAttribute : BaseAttribute {};
[Derived]
[Derived] // Error C3095
ref class Test {};
Clearly this is a bug in the C++/CLI compiler. I don't see it reported at connect.microsoft.com. Doing so yourself probably isn't worth the effort, the language is in maintenance mode.
Possible workarounds are editing the source code of xUnit to assign AllowMultiple again or to create your own InlineDataFixedAttribute that inherits InlineDataAttribute.
来源:https://stackoverflow.com/questions/3116911/attribute-cannot-be-repeated-in-c-cli-but-ok-in-c