Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Ragmaanir/microtest
Small test framework, because it has power asserts as the only assertion.
https://github.com/Ragmaanir/microtest
crystal microtest power-assert test-framework testing
Last synced: about 1 month ago
JSON representation
Small test framework, because it has power asserts as the only assertion.
- Host: GitHub
- URL: https://github.com/Ragmaanir/microtest
- Owner: Ragmaanir
- License: mit
- Created: 2016-05-21T04:30:52.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-01-30T14:52:51.000Z (11 months ago)
- Last Synced: 2024-08-01T17:36:35.133Z (4 months ago)
- Topics: crystal, microtest, power-assert, test-framework, testing
- Language: Crystal
- Homepage:
- Size: 2.52 MB
- Stars: 31
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crystal - microtest - Small opinionated testing library focusing on power asserts (Testing)
- awesome-crystal - microtest - Small opinionated testing library focusing on power asserts (Testing)
- awesome-crystal - microtest - Small opinionated testing library focusing on power asserts (Testing)
README
# 🔬 microtest [![Crystal CI](https://github.com/Ragmaanir/microtest/actions/workflows/crystal.yml/badge.svg)](https://github.com/Ragmaanir/microtest/actions/workflows/crystal.yml)
### Version 1.2.7
A small testing framework inspired by minitest/minitest.cr.
## Features
- This framework is opinionated
- It uses power asserts by default. There are no `assert_equals`, `assert_xyz`, just power asserts (except for `assert_raises`)
- It uses the spec syntax for test case structure (`describe`, `test`, `before`, `after`). Reasons: No test-case name-clashes when using describe. Not forgetting to call super in setup/teardown methods.
- No nesting of describe blocks. IMO nesting of those blocks is an anti-pattern.
- No let-definitions. Only before / after hooks. Use local variables mostly.
- Tests have to be started explicitly by `Microtest.run!`, no at-exit hook.
- Colorized and abbreviated exception stacktraces
- Randomized test order (SEED can be specified as environment variable)
- Focus individual tests (`test! "my test" do ...`)
- Different reportes (progress, descriptions, slow tests)## Installation
Add this to your application's `shard.yml`:
```yaml
development_dependencies:
microtest:
github: ragmaanir/microtest
version: ~> 1.2.7
```And add this to your `spec_helper.rb`:
```crystal
require "microtest"include Microtest::DSL
Microtest.run!
```## Usage
```crystal
class WaterPump
getter name : String
getter speed : Int32
getter? enabled : Bool = falsedef initialize(@name, @speed = 10)
enddef enable
@enabled = true
end
enddescribe WaterPump do
test "enabling" do
p = WaterPump.new("main")
p.enableassert(p.enabled?)
endtest "pump speed" do
p = WaterPump.new("main", speed: 100)assert(p.speed > 50)
endtest "this one is pending since it got no body"
pending "this one is pending even though it has a body" do
raise "should not raise"
end
end```
Run the test with:
`crystal spec`
You can provide the seed to run the tests in the same order:
`SEED=123 crystal spec`
## Power Assert Output
```crystal
describe AssertionFailure do
test "assertion failure" do
a = 5
b = "aaaaaa"
assert "a" * a == b
end
end```
Generates:
![missing](assets/assertion_failure.jpg?raw=true)
### Microtest Test Output (microtest tests using progress reporter)
![missing](assets/spec.jpg?raw=true)
## Reporters
Use common reporter combinations:
```crystal
# both versions include error-list-, slow-tests- and summary-reporters:
Microtest.run!(:progress)
Microtest.run!(:descriptions)
```Or select the used reporters explicitly:
```crystal
Microtest.run!([
Microtest::DescriptionReporter.new,
Microtest::ErrorListReporter.new,
Microtest::SlowTestsReporter.new,
Microtest::SummaryReporter.new,
] of Microtest::Reporter)
``````crystal
describe First do
test "success" do
endtest "skip this"
enddescribe Second do
def raise_an_error
raise "Oh, this is wrong"
endtest "first failure" do
a = 5
b = 7
assert a == b * 2
endtest "error" do
raise_an_error
end
end```
### Progress Reporter
![missing](assets/progress_reporter.jpg?raw=true)### Description Reporter
![missing](assets/description_reporter.jpg?raw=true)### When focus active
```crystal
describe Focus do
test "not focused" do
endtest! "focused" do
endtest "focused too", :focus do
end
end```
![missing](assets/focus.jpg?raw=true)
## Development
Run `./cli readme` to run tests and generate `README.md` from `README.md.ecr` and generate the images of the test outputs (using an alpine docker image).