const { assert, config } = require("chai"); const fc = require('fast-check'); config.truncateThreshold = 0; // To show this failure message nicely, throw `AssertionError` with `assert.fail(message)`. const fastCheckMessage = (o) => o.error.split('\nStack trace:').slice(0, -1).join('').replace(/\n/g, "<:LF:>").replace(/^AssertionError: /, ""); describe("Solution", () => { it("should test for something", () => { fc.assert(fc.property(fc.integer(1, 100), fc.integer(1, 100), (a, b) => { assert.equal(add(a, b), a + b); }), { reporter(o) { if (o.failed) { assert.fail(fastCheckMessage(o)); } } }); }); it("should test for something", () => { fc.assert(fc.property(fc.array(fc.integer(1, 100), 20, 40), fc.array(fc.integer(1, 100), 20, 40), (a, b) => { assert.deepEqual(concat(a, b), [...a, ...b]); }), { reporter(o) { if (o.failed) { assert.fail(fastCheckMessage(o)); } } }); }); });
- const { assert, config } = require("chai");
- const fc = require('fast-check');
- config.truncateThreshold = 0;
- // To show this failure message nicely, throw `AssertionError` with `assert.fail(message)`.
- const fastCheckMessage = (o) =>
- o.error.split('\nStack trace:').slice(0, -1).join('').replace(/\n/g, "<:LF:>").replace(/^AssertionError: /, "");
- describe("Solution", () => {
- it("should test for something", () => {
- fc.assert(fc.property(fc.integer(1, 100), fc.integer(1, 100), (a, b) => {
- assert.equal(add(a, b), a + b);
- }), {
- reporter(o) {
if (o.failed) console.log(`\n<FAILED::>${o.error.split('\nStack trace:').slice(0, -1).join('').replace(/\n/g, "<:LF:>")}`);- if (o.failed) {
- assert.fail(fastCheckMessage(o));
- }
- }
- });
- });
- it("should test for something", () => {
- fc.assert(fc.property(fc.array(fc.integer(1, 100), 20, 40), fc.array(fc.integer(1, 100), 20, 40), (a, b) => {
- assert.deepEqual(concat(a, b), [...a, ...b]);
- }), {
- reporter(o) {
if (o.failed) console.log(`\n<FAILED::>${o.error.split('\nStack trace:').slice(0, -1).join('').replace(/\n/g, "<:LF:>")}`);- if (o.failed) {
- assert.fail(fastCheckMessage(o));
- }
- }
- });
- });
- });
Minimal example for Idris testing with specdris.
module Example
%access export
%default total
add : Nat -> Nat -> Nat
add a b = a + b
module ExampleSpec
-- Tests can be written using [specdris](https://github.com/pheymann/specdris)
-- `specSuite : IO ()` is required.
import Specdris.Spec
import Example
%access export
%default total
specSuite : IO ()
specSuite = spec $ do
describe "add" $ do
it "adds two natural numbers" $ do
(1 `add` 1) `shouldBe` 2
Experiments to add test support for Forth (Codewars/codewars-runner-cli#625).
The test framework part was originally contributed by @nomennescio.
\ Test Framework (ttester + extension)
decimal
s" test/ttester.fs" included
: #ms ( dmicroseconds -- len c-addr ) <# # # # [char] . hold #s #> ;
: describe#{ ( len c-addr -- ) cr ." <DESCRIBE::>" type cr utime ;
: it#{ ( len c-addr -- ) cr ." <IT::>" type cr utime ;
: }# ( -- ) utime cr ." <COMPLETEDIN::>" 2swap d- #ms type ." ms" cr ;
create EXPECTED-RESULTS 32 cells allot
variable RESULTS
variable DIFFERENCES
: <{ T{ ;
: }>
depth ACTUAL-DEPTH @ = if
depth START-DEPTH @ > if
depth START-DEPTH @ - dup RESULTS ! 0 do
dup EXPECTED-RESULTS i cells + !
ACTUAL-RESULTS i cells + @ <> DIFFERENCES +!
loop
DIFFERENCES @ if
cr ." <FAILED::>expected: "
RESULTS @ 0 do EXPECTED-RESULTS i cells + @ . loop
." <:LF:> actual: "
RESULTS @ 0 do ACTUAL-RESULTS i cells + @ . loop
cr
else
cr ." <PASSED::>Test Passed" cr
then
then
else
cr ." <FAILED::>Wrong number of results. Expected:<:LF:>" ACTUAL-DEPTH @ . ." <:LF:>got:<:LF:>" depth . cr
then
F} ;
\ Solution
: solution ( a b -- a*b ) * ;
\ Tests
s" Basic Tests" describe#{
s" zeros" it#{
<{ 0 0 solution -> 0 }>
<{ 0 1 solution -> 0 }>
<{ 1 0 solution -> 0 }>
}#
s" non-zeros" it#{
\ intentionally broken tests
<{ 1 1 solution -> 2 }>
<{ 3 5 solution -> 8 }>
}#
}#
Testing
Frameworks
Proposed expect_error
deployed on preview.
@test.describe('expect_error is backwards compatible') def d1(): for f in (f0, f1, f2, f3): test.expect_error('Should raise something', f) excn = ('Exception', 'ArithmeticError', 'ZeroDivisionError', 'LookupError', 'KeyError', 'OSError') exc = (Exception, ArithmeticError, ZeroDivisionError, LookupError, KeyError, OSError) @test.describe('expect_error with exception class') def d2(): @test.it('f0 raises nothing') def i0(): test.expect_error('f0 did not raise any exception', f0) for i in range(6): test.expect_error('f0 did not raise {}'.format(excn[i]), f0, exc[i]) @test.it('f1 raises Exception') def i1(): test.expect_error('f1 did not raise Exception', f1) for i in range(6): test.expect_error('f1 did not raise {}'.format(excn[i]), f1, exc[i]) @test.it('f2 raises Exception >> ArithmeticError >> ZeroDivisionError') def i2(): test.expect_error('f2 did not raise Exception', f2) for i in range(6): test.expect_error('f2 did not raise {}'.format(excn[i]), f2, exc[i]) @test.it('f3 raises Exception >> LookupError >> KeyError') def i3(): test.expect_error('f3 did not raise Exception', f3) for i in range(6): test.expect_error('f3 did not raise {}'.format(excn[i]), f3, exc[i])
@test.describe('expect_error, previous version')- @test.describe('expect_error is backwards compatible')
- def d1():
- for f in (f0, f1, f2, f3):
- test.expect_error('Should raise something', f)
def expect_error(message, function, class_=Exception):passed = Falsetry: function()except class_: passed = Trueexcept: passtest.expect(passed, message)- excn = ('Exception', 'ArithmeticError', 'ZeroDivisionError', 'LookupError', 'KeyError', 'OSError')
- exc = (Exception, ArithmeticError, ZeroDivisionError, LookupError, KeyError, OSError)
@test.describe('expect_error, new version')- @test.describe('expect_error with exception class')
- def d2():
- @test.it('f0 raises nothing')
- def i0():
expect_error('f0 did not raise any exception', f0)- test.expect_error('f0 did not raise any exception', f0)
- for i in range(6):
expect_error('f0 did not raise {}'.format(excn[i]), f0, exc[i])- test.expect_error('f0 did not raise {}'.format(excn[i]), f0, exc[i])
- @test.it('f1 raises Exception')
- def i1():
expect_error('f1 did not raise Exception', f1)- test.expect_error('f1 did not raise Exception', f1)
- for i in range(6):
expect_error('f1 did not raise {}'.format(excn[i]), f1, exc[i])- test.expect_error('f1 did not raise {}'.format(excn[i]), f1, exc[i])
- @test.it('f2 raises Exception >> ArithmeticError >> ZeroDivisionError')
- def i2():
expect_error('f2 did not raise Exception', f2)- test.expect_error('f2 did not raise Exception', f2)
- for i in range(6):
expect_error('f2 did not raise {}'.format(excn[i]), f2, exc[i])- test.expect_error('f2 did not raise {}'.format(excn[i]), f2, exc[i])
- @test.it('f3 raises Exception >> LookupError >> KeyError')
- def i3():
expect_error('f3 did not raise Exception', f3)- test.expect_error('f3 did not raise Exception', f3)
- for i in range(6):
expect_error('f3 did not raise {}'.format(excn[i]), f3, exc[i])- test.expect_error('f3 did not raise {}'.format(excn[i]), f3, exc[i])