TNG / ArchUnitNET

A C# architecture test library to specify and assert architecture rules in C# for automated testing.
Apache License 2.0
826 stars 55 forks source link

HaveUnitTests - Extension Method #226

Closed medoni closed 3 months ago

medoni commented 6 months ago

As an Software Architect I want to specify which Layers have to be unit tested. For doing this, I've written an extension method to the Should() fluent api.

[TestFixture]
[Category("Architecture")]
public class PublicMethodsShouldHaveUnitTestFixture
{
    [Test]
    public void Verify()
    {
        PublicMethodsShouldHaveUnitTestRule.Check(ExampleServiceArchitecture.Architecture);
    }

    private static readonly IArchRule PublicMethodsShouldHaveUnitTestRule =
        MethodMembers()
            .That()
            .ArePublic().And()
            .AreDeclaredIn(ExampleServiceArchitecture.ClassesThatShouldBeTested).And()
            .AreNoConstructors().And()
            .DoNotHaveNameStartingWith("get_").And()
            .DoNotHaveNameStartingWith("set_")
            .As("Public methods need Unit Tests. See LINK_TO_WIKI.")
        .Should()
            .HaveUnitTests();
}

ClassesThatShouldBeTested can be defined as follows:

ClassesThatShouldBeTested =
    Classes()
    .That()
    .Are(DomainLayer).Or()
    .Are(PersistenceLayer).Or()
    .Are(CommandHandler).Or()
    .Are(EventHandler)
    .As("Public classes that should be tested");

Does it make sense to create a pull request for this extension? What are your requirements for such a helper method?

The full example can be see in here:

Including tests

mak638 commented 3 months ago

Hi @medoni, first of all thank you very much for your interest in the project and your contribution.

We have looked into your extension methods but although we think that these might be perfectly suitable for you and your project, we also believe that they would be too hard to generalize. As of now they simply rely on the naming conventions for classes/methods and corresponding tests in your project - in another project there might not even be a naming convention that could be used or the tests could indicate that unit tests exist just based on the naming even if they don't.

In general, if you would like to see which classes and methods in your project are unittested, we would recommend to use a dedicated code coverage tool as this would be a much better fit for this specific usecase than ArchUnitNet.