CodeSheriff is a simple library that helps you in writing JUnit tests that check the quality of your code

Overview

CodeSheriff

Build Status codecov

CodeSheriff is a simple library that helps you in writing JUnit tests that check the quality of your code. For example, CodeSheriff may fail because you have methods in your code that have more than X lines of code, or that have complexity greater than Y.

CodeSheriff is highly flexible and lets you define the quality rules through a very simple DSL. No more complicated configurations in complicated code quality tools. Just write a test!

This framework was highly inspired by ArchUnit. If you want to write unit tests to check architectural conformance, check that framework!

Note that this is still just a prototype and is not yet battle tested. Help me out here!

The CodeSheriff API

The best way to understand CodeSheriff is by means of examples. See the test class below:

import com.github.mauricioaniche.codesheriff.dsl.CodeSheriff;
import com.github.mauricioaniche.codesheriff.junit.CodeSheriffJUnit5;

public class SheriffRunnerJUnitTest extends CodeSheriffJUnit5 {

    // it can be a method that returns 'CodeSheriff' ...
    CodeSheriff complexity() {
        CodeSheriff sheriff = new CodeSheriff();

        sheriff.thatEnsures()
                .methods()
                .inClassesOfPackage("a.b.c")
                .have()
                .complexity(m -> m < 10);

        return sheriff;
    }

    // ... or a field!
    CodeSheriff loc = new CodeSheriff()
            .thatEnsures()
            .methods()
            .inClassesOfPackage("fixture.f3")
            .have()
            .linesOfCode(m -> m < 100);

}

For CodeSheriff to work, all you need to do it:

  • Create a class that extends from the provided CodeSheriffJUnit class.
  • Write as many _CodeSheriff_s as you want. A CodeSheriff is basically a rule that the library will check later.
  • See the complexity rule. It ensures that all methods within the a.b.c package have complexity of less than 10. Complexity here is basically cyclomatic complexity (i.e., the number of ifs, fors, while, etc, in your code)
  • See the loc rule. It ensures that all classes in the a.b.c package have less than 100 lines of code.

This class is a JUnit 5 test class. Just run it. If no rules are broken in your code, then, the test passes. If a rule is broken, CodeSheriff will list you all the classes and methods that break a rule.

How to import it in my project?

Simply add it to your Maven or Gradle file:

<dependency>
    <groupId>com.github.mauricioanichegroupId>
    <artifactId>codesheriffartifactId>
    <version>0.2.0version>
    <scope>testscope>
dependency>

How does it work?

CodeSheriff builds the AST of your entire source code and collect code metrics. This is done by my other library, CK.

This means that checking the rules may take a while depending on the size of your project.

Available rules

Just use the DSL to explore all the options.

License

This code is licensed under the MIT license.

You might also like...

A sample repo to help you handle basic auth for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

A sample repo to help you handle basic auth for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to handle basic auth for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows - htt

Jul 13, 2022

A sample repo to help you clear browser cache with Selenium 4 Java on LambdaTest cloud. Run your Java Selenium tests on LambdaTest platform.

A sample repo to help you clear browser cache with Selenium 4 Java on LambdaTest cloud. Run your Java Selenium tests on LambdaTest platform.

How to clear browser cache with Selenium 4 Java on LambdaTest cloud Prerequisites Install and set environment variable for java. Windows - https://www

Jul 13, 2022

A sample repo to help you run automation test in incognito mode in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

A sample repo to help you run automation test in incognito mode in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to run automation test in incognito mode in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows - htt

Jul 13, 2022

A sample repo to help you handle cookies for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

A sample repo to help you handle cookies for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to handle cookies for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows - https:

Jul 13, 2022

A sample repo to help you set geolocation for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

A sample repo to help you set geolocation for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to set geolocation for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows - https

Jul 13, 2022

A sample repo to help you capture JavaScript exception for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

A sample repo to help you capture JavaScript exception for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to capture JavaScript exception for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Wi

Jul 13, 2022

A sample repo to help you find an element by text for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

A sample repo to help you find an element by text for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to find an element by text for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows

Jul 13, 2022

A sample repo to help you emulate network conditions in Java-selenium automation test on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

A sample repo to help you emulate network conditions in Java-selenium automation test on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to emulate network conditions in Java-selenium automation test on LambdaTest Prerequisites Install and set environment variable for java. Windows

Jul 13, 2022

πŸ”Œ Simple library to manipulate HTTP requests/responses and capture network logs made by the browser using selenium tests without using any proxies

πŸ”Œ Simple library to manipulate HTTP requests/responses and capture network logs made by the browser using selenium tests without using any proxies

Simple library to manipulate HTTP requests and responses, capture the network logs made by the browser using selenium tests without using any proxies

Oct 23, 2022
Comments
  • Ignore hidden directories

    Ignore hidden directories

    It seems that codesheriff reads things from hidden directories, like .idea, and it makes it crash.

    We should make sure hidden directories are ignored.

    opened by mauricioaniche 2
Owner
MaurΓ­cio Aniche
Assistant Professor in Software Engineering at TU Delft (@SERG-Delft). Often too practical for academia and too theoretical for software companies.
MaurΓ­cio Aniche
IntelliJ IDEA and JUnit: Writing, Finding, and Running Tests

IntelliJ IDEA and JUnit: Writing, Finding, and Running Tests ?? Webinar https://blog.jetbrains.com/idea/2021/11/live-stream-recording-intellij-idea-an

Christian Stein 11 Jul 23, 2022
Serenity BDD is a test automation library designed to make writing automated acceptance tests easier, and more fun.

That feeling you get when you know you can trust your tests Serenity BDD is a library designed to make writing automated acceptance tests easier, and

Serenity BDD 654 Dec 28, 2022
Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.

Testcontainers Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium we

null 6.7k Jan 9, 2023
PGdP-Tests-WS21/22 is a student-created repository used to share code tests.

PGdP-Tests-WS21-22 PGdP-Tests-WS21/22 is a student-created repository used to share code tests. Important Note: In the near future, most exercises wil

Jonas Ladner 56 Dec 2, 2022
Beagle helps you identify keywords, phrases, regexes, and complex search queries of interest in streams of text documents.

Beagle Beagle is a detector of interesting things in text. Its intended use is in-stream search applications. Suppose you need to monitor a stream of

TokenMill 49 Dec 3, 2022
This is a repository to collect JUnit Tests for GAD exercises at TUM in SS21

TUM_GAD_Tests_SS21 This is a repository to collect JUnit Tests for GAD exercises at TUM in SS21. These tests have been written by students for student

null 41 Oct 31, 2022
Intercept network request by running Selenium tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog β‹… Docs β‹… Learning Hub β‹… Newsletter β‹… Certifications β‹… YouTube       Learn how to use JUnit framewor

null 11 Jul 11, 2022
Emulate geolocation by running Selenium tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog β‹… Docs β‹… Learning Hub β‹… Newsletter β‹… Certifications β‹… YouTube       Learn how to use JUnit framewor

null 11 Jul 11, 2022
Run Selenium 4 tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog β‹… Docs β‹… Learning Hub β‹… Newsletter β‹… Certifications β‹… YouTube       Learn how to use JUnit framewor

null 11 Jul 11, 2022
Clear browser cache by running Selenium tests with JUnit on LambdaTest cloud.

Run Selenium Tests With JUnit On LambdaTest (Browser Cache Clearing Example) Blog β‹… Docs β‹… Learning Hub β‹… Newsletter β‹… Certifications β‹… YouTube      

null 16 Jul 11, 2022