Brutal 💎🔨

Version Yard documentation CI 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"

And then execute:

bundle install

Or install it yourself as:

gem install brutal

Quick Start

Just type brutal in a Ruby project's folder and watch the magic happen.

Usage

The brutal.yml file is a manifest you can use to define your Brutal meta-spec. It has 4 top-level sections:

  • header - Specifies the code to execute before generating the test suite.
  • 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.

Getting started

  1. Create a brutal.yml file in your application's root directory. The following example brutal.yml defines the shape of a Hello test suite:
---
subject: |
  "Hello " + "%{string}"

contexts:
  string:
    - Alice
    - Bob

actuals:
  - "%{subject}.to_s"
  - "%{subject}.length"
  1. Run the brutal command from the same directory.

  2. Read the generated test.rb file in the same directory:

# Brutal test suite

# ------------------------------------------------------------------------------

actual = begin
  "Hello " + "Alice"
end

raise if actual.to_s != "Hello Alice"
raise if actual.length != 11

# ------------------------------------------------------------------------------

actual = begin
  "Hello " + "Bob"
end

raise if actual.to_s != "Hello Bob"
raise if actual.length != 9

More examples

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

Rake integration example

A generated test.rb file could be matched as follows:

Rake::TestTask.new do |t|
  t.pattern = "test.rb"
end

Test suite

Brutal's test set is brutally self-generated here: ./test.rb

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:
Sashite