Code Monkey home page Code Monkey logo

minitest's Introduction

Minitest

A mini testing framework cross-compiled for Scala 2.10, 2.11, 2.12 and Scala.js 0.6.x.

Build Status

Usage in SBT

For build.sbt (use the %%% operator for Scala.js):

// use the %%% operator for Scala.js
libraryDependencies += "io.monix" %% "minitest" % "2.1.1" % "test"

testFrameworks += new TestFramework("minitest.runner.Framework")

In case you want the optional package for ScalaCheck integration:

// use the %%% operator for Scala.js
libraryDependencies += "io.monix" %% "minitest-laws" % "2.1.1" % "test"

Tutorial

Test suites MUST BE objects, not classes. To create a simple test suite, it could inherit from SimpleTestSuite:

Here's a simple test:

import minitest._

object MySimpleSuite extends SimpleTestSuite {
  test("should be") {
    assertEquals(2, 1 + 1)
  }

  test("should not be") {
    assert(1 + 1 != 3)
  }

  test("should throw") {
    class DummyException extends RuntimeException("DUMMY")
    def test(): String = throw new DummyException

    intercept[DummyException] {
      test()
    }
  }

  test("test result of") {
    assertResult("hello world") {
      "hello" + " " + "world"
    }
  }
}

In case you want to setup an environment for each test and need setup and tearDown semantics, per test, you could inherit from TestSuite. Then on each test definition, you'll receive a fresh value:

import minitest.TestSuite

object MyTestSuite extends TestSuite[Int] {
  def setup(): Int = {
    Random.nextInt(100) + 1
  }

  def tearDown(env: Int): Unit = {
    assert(env > 0, "should still be positive")
  }

  test("should be positive") { env =>
    assert(env > 0, "positive test")
  }

  test("should be lower or equal to 100") { env =>
    assert(env <= 100, s"$env > 100")
  }
}

Some tests require setup and tear down logic to happen only once per test suite being executed and TestSuite supports that as well, but note you should abstain from doing this unless you really need it, since the per test semantics are much saner:

object MyTestSuite extends TestSuite[Int] {
  private var system: ActorSystem = _

  override def setupSuite(): Unit = {
    system = ActorSystem.create()
  }

  override def tearDownSuite(): Unit = {
    TestKit.shutdownActorSystem(system)
    system = null
  }
}

Minitest supports asynchronous results in tests, just use testAsync and return a Future[Unit]:

import scala.concurrent.ExecutionContext.Implicits.global

object MySimpleSuite extends SimpleTestSuite {
  testAsync("asynchronous execution") {
    val future = Future(100).map(_+1)
    
    for (result <- future) yield {
      assertEquals(result, 101)
    }
  }
}

Minitest has integration with ScalaCheck. So for property-based testing:

import minitest.laws.Checkers

object MyLawsTest extends SimpleTestSuite with Checkers {
  test("addition of integers is commutative") {
    check2((x: Int, y: Int) => x + y == y + x)
  }
  
  test("addition of integers is transitive") {
    check3((x: Int, y: Int, z: Int) => (x + y) + z == x + (y + z))
  }
}

That's all you need to know.

License

All code in this repository is licensed under the Apache License, Version 2.0. See LICENCE.

Copyright © 2014-2016 Alexandru Nedelcu

minitest's People

Contributors

alexandru avatar sethtisue avatar eikek avatar gabro avatar 2m avatar m-sp avatar tindzk avatar xuwei-k avatar

Watchers

James Cloos avatar  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.