GithubHelp home page GithubHelp logo

fuchu's Introduction

Fuchu

Fuchu is a test library for .NET, supporting C# and VB.NET but with a special focus on F#.
It draws heavily from Haskell's test-framework and HUnit.
You can read about the rationale and underlying concepts in this blog post.

Binaries

Binaries are available on NuGet.

Writing tests

Here's the simplest test possible:

open Fuchu

let simpleTest = 
    testCase "A simple test" <| 
        fun _ -> Assert.Equal("2+2", 4, 2+2)

Tests can be grouped (with arbitrary nesting):

let tests = 
    testList "A test group" [
        testCase "one test" <|
            fun _ -> Assert.Equal("2+2", 4, 2+2)
        testCase "another test" <|
            fun _ -> Assert.Equal("3+3", 3, 3+3)
    ]

In C#:

static Test ATest {
    get {
        return Test.List("A test group", new[] {
            Test.Case("one test", () => Assert.Equal("2+2", 4, 2+2)),
            Test.Case("another test", () => Assert.Equal("3+3", 3, 3+3)),
        });
    }
}

The first parameter in the assertions describes the assertion. This is usually an optional parameter in most test frameworks; in Fuchu it's required to foster descriptive failures, so you'll get a failure like "3+3 Expected value 3, actual 6" instead of just "Expected value 3, actual 6".

For more examples, including a few ways to do common things in other test frameworks like setup/teardown and parameterized tests, see the F# tests and the C# tests

Assertions

Fuchu is mainly oriented to test organization. Although it does have a few basic assertions, you're encouraged to write your own specialized assertions for each project (they're only a couple of lines in F#), or use some other library for assertions, like Unquote, FsUnit, or even MbUnit or NUnit.

Running tests

The test runner is the test assembly itself. It's recommended to compile your test assembly as a console application. You can run a test directly like this:

run simpleTest // or runParallel

which returns 1 if any tests failed, otherwise 0. Useful for returning to the operating system as error code. Or you can mark the top-level test in each test file with the [<Tests>] attribute, then define your main like this:

[<EntryPoint>]
let main args = defaultMainThisAssembly args

This defaultMainThisAssembly function admits a "/m" parameter passed through the command-line to run tests in parallel.

You can single out tests by filtering them by name. For example:

tests
|> Test.filter (fun s -> s.EndsWith "another test")
|> run

You can use the F# REPL to run tests this way.

In C#:

static int Main(string[] args) {
    return ATest.Run(); // or RunParallel()
}

Or scanning for tests marked with the [Tests] attribute:

static int Main(string[] args) {
    return Tests.DefaultMainThisAssembly(args);
}

FsCheck integration

Reference FsCheck and Fuchu.FsCheck to test properties:

let config = { FsCheck.Config.Default with MaxTest = 10000 }

let properties = 
    testList "FsCheck" [
        testProperty "Addition is commutative" <|
            fun a b -> 
                a + b = b + a
        
        // you can also override the FsCheck config
        testPropertyWithConfig config "Product is distributive over addition" <|
            fun a b c -> 
                a * (b + c) = a * b + a * c
    ]

run properties

In C# (can't override FsCheck config at the moment):

static Test Properties =
    Test.List("FsCheck", new[] {
        FsCheck.Property("Addition is commutative",
                            (int a, int b) => a + b == b + a),
        FsCheck.Property("Product is distributive over addition",
                            (int a, int b, int c) => a * (b + c) == a * b + a * c),
    });

You can freely mix FsCheck properties with regular test cases and test lists.

More examples

Some projects using Fuchu:

fuchu's People

Contributors

mausch avatar haf avatar cnd avatar

Stargazers

 avatar

Watchers

 avatar Ademar Gonzalez avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.