Skip to content
/ brutal Public

A code-first approach to automate the writing of unit tests.

License

Notifications You must be signed in to change notification settings

fixrb/brutal

Repository files navigation

Brutal 💎🔨

Version Yard documentation Ruby RuboCop License

A code-first approach to automate the writing of unit tests.

Intro

I Hate Tests

I don't like tests. It's not DRY. -- Matz

Overview

Let Brutal craft for you in no time a (potentially huge) framework-less vanilla Ruby file describing the actual behavior of your code across as many context combinations as necessary.

By delegating to Brutal the repetitive (and redundant) task of writing tests, you will be able to focus on your core business: the code itself.

Brutal-Driven Development

Warning

The Brutal-Driven Development process does not prevent bugs from appearing in the code.

A generated test suite acts as a picture of the code's behavior. Therefore, if the code is wrong, the picture of the code's behavior will also be wrong.

The mere fact that all expectations are true does not mean that the code behaves as it should.

It is therefore the responsibility of the developer to analyze the generated behavioral pictures to ensure that the code reacts as it is supposed to according to the contexts in which it is evaluated.

Installation

Add this line to your application's Gemfile:

gem "brutal", ">= 1.6.0.beta4", require: false

And then execute:

bundle install

Or install it yourself as:

gem install brutal --pre

Usage

YAML manifest

Brutal needs configuration files in YAML format to know how to write tests. Configuration file names are suffixed by _brutal.yaml and composed of 7 top-level sections:

  • header - Specifies a block of lines to be executed once before all examples.
  • before - Specifies a block of lines to be executed before each example.
  • subject - Specifies the template of the code to be declined across contexts.
  • contexts - Specifies a list of variables to populate the subject's template.
  • actuals - Specifies templates to challenge evaluated subjects & get results.
  • after - Specifies a block of lines to be executed after each example.
  • footer - Specifies a block of lines to be executed once after all examples.

Command line

The brutal command comes with several options you can use to customize Brutal's behavior.

For a full list of options, run the brutal command with the --help flag:

brutal --help
Usage: brutal [options] [files or directories]

    --format=FORMAT Choose "ruby" (default).
    --help          Display this help.
    --version       Display the version.

Assuming that in the workspace there is a configuration file named user_brutal.yaml, the test suite can be generated via one of these commands:

brutal user_brutal.yaml

or:

brutal .

or even:

brutal

This would create a test_user.rb file containing the test suite.

Assuming now that in the workspace there are a large number of configuration files named in the spec/ folder, the complete test suite could be generated recursively via this command:

brutal spec/

This would create one test file per configuration file matching ./spec/**/*_brutal.yaml in to ./spec/**/test_*.rb.

Some examples

https://github.com/fixrb/brutal/tree/main/examples/

Rake integration example

Generated test suite files could be matched as follows:

Rake::TestTask.new do |t|
  t.pattern = "**/test_*.rb"
end

Contact

Versioning

Brutal follows Semantic Versioning 2.0.

License

The gem is available as open source under the terms of the MIT License.


This project is sponsored by Sashité:

Sashité logo Sashité logo