Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip family properties cause error in custom attributes #3071

Open
behrouz-rad opened this issue Dec 1, 2024 · 4 comments
Open

Skip family properties cause error in custom attributes #3071

behrouz-rad opened this issue Dec 1, 2024 · 4 comments

Comments

@behrouz-rad
Copy link

behrouz-rad commented Dec 1, 2024

If you create a custom FactAttribute (or even a simple attribute) and apply it to a test method, using the Skip family properties results in an error in the test runner instead of displaying a warning that the test was skipped.
Using Skip family properties directly in test methods works as expected.

// Attribute
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class MyAttribute : FactAttribute
{
  public MyAttribute()
  {
    Assert.SkipUnless(1==2, "My Reason");
  }
}

// Test method
[My]
public void My_Test_Method()
{
}

// Error in test runner:
Stack Trace: 
MyAttribute.ctor() line 14
CustomAttribute._CreateCaObject(RuntimeModule pModule, RuntimeType type, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
CustomAttribute.GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit)
Attribute.GetCustomAttributes(MemberInfo element, Boolean inherit)
Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
Lazy`1.CreateValue()
@bradwilson
Copy link
Member

That's not how that's intended to be used. Assert.SkipUnless is for use within the test, not with the attribute.

FactAttribute has a settable SkipUnless property that's intended to point to a static public property that will be read at the appropriate time. So what you want is this:

public class MyAttribute : FactAttribute
{
    public MyAttribute()
    {
        Skip = "My Reason";
        SkipUnless = nameof(ShouldRun);
    }

    public static bool ShouldRun => 1 == 2;
}

@bradwilson
Copy link
Member

Also, you don't need a custom attribute for this. Instead, you could just do this:

public static bool ShouldRun => 1 == 2;

[Fact(Skip = "My Reason", SkipUnless = nameof(ShouldRun))]
public void My_Test_Method()
{
}

@behrouz-rad
Copy link
Author

behrouz-rad commented Dec 1, 2024

That's not how that's intended to be used. Assert.SkipUnless is for use within the test, not with the attribute.

FactAttribute has a settable SkipUnless property that's intended to point to a static public property that will be read at the appropriate time. So what you want is this:

public class MyAttribute : FactAttribute
{
    public MyAttribute()
    {
        Skip = "My Reason";
        SkipUnless = nameof(ShouldRun);
    }

    public static bool ShouldRun => 1 == 2;
}

Thanks for the answer.

Something strange happens with this code.

It seems that xUnit expects to see ShouldRun in the test class as well. This means I have to define ShouldRun twice: once in the custom attribute class and once in the test class. However, only the one in the test class will be considered!

Cannot find public static property 'ShouldRun' on type 'MyProject.MyTestClass' for dynamic skip on test method 'MyProject.MyTestClass.MyTest'

@bradwilson
Copy link
Member

bradwilson commented Dec 1, 2024

Sorry, I forgot to mention that if you're putting it in the attribute, you need to use SkipType as well (set it to the attribute type). The default for SkipType when unset is to look for the property on the test class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants