Skip to content

Conversation

@4creators
Copy link
Contributor

as test methods in xunit: "public static" and "public abstract"

PublicMethodShouldBeMarkedAsTest greedily issues warning for 'public static' and 'public abstract'
methods although they can not be used as a test method in xunit framework. Introduced code changes
verify if public method is abstract or is static and if detected skip further checks.

Fixes xunit/xunit#1466

…d in xunit: static and abstract

PublicMethodShouldBeMarkedAsTest greedily issues warning for 'public static' and 'public abstract'
methods although they can not be used as a test method in xunit framework. Introduced code changes
verify if public method is abstract or is static and if detected skip further checks.
@dnfclas
Copy link

dnfclas commented Sep 22, 2017

@4creators,
Thanks for having already signed the Contribution License Agreement. Your agreement was validated by .NET Foundation. We will now review your pull request.
Thanks,
.NET Foundation Pull Request Bot

@4creators
Copy link
Contributor Author

PTAL @marcind

<Prerequisites>
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[15.0.26208.0,16.0)" DisplayName="Visual Studio core editor" />
</Prerequisites>
</PackageManifest>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not reformat files. The entire file looks changed because of spacing differences.


var method = (IMethodSymbol)member;
if (method.MethodKind != MethodKind.Ordinary)
if (method.MethodKind != MethodKind.Ordinary || method.IsStatic || method.IsAbstract)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xUnit.net does support public static test methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I have tested initially and xunit failed to detect that but using xunit.analyzers.tests I got it as You said. My wrong

<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="xunit.analyzers..d09124d1-9c5e-40de-b1de-35120196c3e0" Version="1.0" Language="en-US" Publisher="Marcin Dobosz"/>
<Identity Id="xunit.analyzers..d09124d1-9c5e-40de-b1de-35120196c3e0" Version="1.0.1" Language="en-US" Publisher="Marcin Dobosz"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this file changed at all? Please undo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought that xunit has semantic versioning. Change in error/warning detection syntax changes behaviour so I have incremented build version change.

Obviously will change it as requested.

@4creators
Copy link
Contributor Author

Addressed all review remarks
@bradwilson @marcind


var method = (IMethodSymbol)member;
if (method.MethodKind != MethodKind.Ordinary)
if (method.MethodKind != MethodKind.Ordinary || method.IsAbstract)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ever true? We returned earlier if type.IsAbstract was true, so logically this must be a concrete type and none of its members can be abstract.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the best fix would be to remove the type.IsAbstract check above and keep this as-is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for late replay, I was stuck with other work for a while.
This way it is set it can skip diagnostics in partially invalid code during code editing i.e. concrete class with abstract method defined or skip analysis in abstract class for public concrete methods.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, it's fine to leave this as-is then, but can you add a comment about it?

{
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"public abstract class TestClass {
[Xunit.Fact] public void TestMethod() { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Rename to AbstractMethod, since it isn't a test method and it doesn't raise an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

}");

Assert.Empty(diagnostics);
}
Copy link
Contributor

@jamesqo jamesqo Sep 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to also add this test:

        [Fact]
        public async void DoesNotFindErrorForPublicAbstractMethodMarkedWithFact()
        {
            var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"public abstract class TestClass {
    [Xunit.Fact] public void TestMethod() { }
    [Xunit.Fact] public abstract void AbstractTestMethod();
}");
            Assert.Empty(diagnostics);
        }

I can't test right now but that should work fine at runtime, since the [Fact] attribute is inherited on overriding members.

Copy link
Contributor Author

@4creators 4creators Oct 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK it makes sense

@4creators
Copy link
Contributor Author

PTAL
@bradwilson @marcind @jamesqo
I left one unresolved problem to decide see: #82 (comment)

@jamesqo
Copy link
Contributor

jamesqo commented Oct 15, 2017

After you leave a comment LGTM

Copy link
Member

@bradwilson bradwilson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 once the requested comment has been made

@4creators
Copy link
Contributor Author

OK All feedback addressed 😄
PTAL @bradwilson

@marcind marcind merged commit cdf2f86 into xunit:master Oct 15, 2017
@marcind
Copy link
Member

marcind commented Oct 15, 2017

Thanks for the submission

dougbu added a commit to aspnet/AspNetWebStack that referenced this pull request Jan 31, 2023
Bump test project dependencies
- Castle.Core, Moq, and xUnit versions were all out of date
- hold xunit.runner.visualstudio version back in .NET SDK test projects
  - can be moved to latest version after we stop testing on netcoreapp2.1
- add missing xunit.analyzers reference to System.Web.Razor.Test project
- move all test projects to .NET v4.6.2 (a supported framework)
  - remove `netstandard` reference in System.Net.Http.Formatting.Test; not needed w/ new TFM & updated references
- further separate build of Microsoft.TestCommon project when invoked from NetCore.Test project
  - special case `RestorePackages` for this case
  - add System.Net.Http references to avoid conflicting versions e.g. src/ and test/ TFMs differ

React to changed xUnit APIs
- adjust Microsoft.TestCommon code
- nit: use `Array.Empty<byte>()` in `TranscodingStreamTests`
  - `TranscodingStream` `internal`s can be `private` instead

Resolve xUnit issues new analyzers find
- address xUnit2000 warnings
  - pass expected values to `Assert.Equal(...)` as correct (left) argument
- make generic method types explicit to avoid
  `error CS0121: The call is ambiguous between the following methods or properties: ...`
- note: cannot remove unnecessary xUnit1013 suppression
  - related bug (xunit/xunit#1466) apparently not fixed in 1.0.0 analyzers package
  - was xunit/xunit.analyzers#82 fix (in 2017) insufficient?

React to new Moq changes
- avoid `ProtectedMock\`1.ThrowIfPublicMethod(...)` `throw`ing
  - use new overloads introduced in the Moq 4.11.0 release
- adjust to Moq hiding the `ObjectProxy` type better
- update `ControllerContext` mocking to avoid NREs
  - setting `HttpContext.User` on the `Object` left `get` value `null` (did nothing)
- nit: use `SetupGet(...)` for another property
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants