Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/typelevel/cats-effect-testing
Integration between cats-effect and test frameworks
https://github.com/typelevel/cats-effect-testing
cats cats-effect minitest scala scalatest specs2 utest
Last synced: 5 days ago
JSON representation
Integration between cats-effect and test frameworks
- Host: GitHub
- URL: https://github.com/typelevel/cats-effect-testing
- Owner: typelevel
- License: apache-2.0
- Created: 2019-06-26T14:59:32.000Z (over 5 years ago)
- Default Branch: series/1.x
- Last Pushed: 2024-10-28T12:11:49.000Z (about 2 months ago)
- Last Synced: 2024-10-29T21:05:55.369Z (about 2 months ago)
- Topics: cats, cats-effect, minitest, scala, scalatest, specs2, utest
- Language: Scala
- Homepage:
- Size: 735 KB
- Stars: 191
- Watchers: 16
- Forks: 42
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# cats-effect-testing
A quickie little utility which makes it easier to write tests using [specs2](https://specs2.org) (mutable or functional), [scalatest](http://scalatest.org), [µTest](https://github.com/lihaoyi/utest) or [minitest](https://github.com/monix/minitest) where the examples are effectful within `cats.effect.IO`.
## Specs2
```scala
import cats.effect.IO
import cats.effect.testing.specs2.CatsEffect
import org.specs2.mutable.Specification// for some reason, only class works here; object will not be detected by sbt
class ExampleSpec extends Specification with CatsEffect {
"examples" should {
"do the things" in IO {
true must beTrue
}
}
}
```The above compiles and runs exactly as you would expect.
By default, tests run with a 10 second timeout. If you wish to override this, simply override the inherited `Timeout` val:
```scala
override val Timeout = 5.seconds
```If you need an `ExecutionContext`, one is available in the `executionContext` val.
And if you need to share Cats Effect's Resource between test examples you can extend `CatsResource`, like so:
```scala
import cats.effect.{IO, Ref, Resource}
import org.specs2.mutable.SpecificationLikeclass CatsResourceSpecs extends CatsResource[IO, Ref[IO, Int]] with SpecificationLike {
sequentialval resource: Resource[IO, Ref[IO, Int]] =
Resource.make(Ref[IO].of(0))(_.set(Int.MinValue))"cats resource support" should {
"run a resource modification" in withResource { ref =>
ref.modify{ a =>
(a + 1, a)
}.map(
_ must_=== 0
)
}"be shared between tests" in withResource { ref =>
ref.modify{ a =>
(a + 1, a)
}.map(
_ must_=== 1
)
}
}
}
```The Resource is acquired before the tests are run and released afterwards. A more realistic example would be to share a Resource that takes a long time to start up.
### Usage
```sbt
libraryDependencies += "org.typelevel" %% "cats-effect-testing-specs2" % "" % Test
```Published for Scala 3.1+, 2.13, 2.12, as well as ScalaJS 1.7+. Depends on Cats Effect 3.1+ and Specs2 4.13.x. Specs2 5.0 is not yet supported.
Early versions (`0.x.y`) were published under the `com.codecommit` groupId.
## ScalaTest
```scala
import cats.effect._
import cats.effect.testing.scalatest.AsyncIOSpec
import org.scalatest.matchers.should.Matchers
import org.scalatest.freespec.AsyncFreeSpecclass MySpec extends AsyncFreeSpec with AsyncIOSpec with Matchers {
"My Code " - {
"works" in {
IO(1).asserting(_ shouldBe 1)
}
}```
### Usage```sbt
libraryDependencies += "org.typelevel" %% "cats-effect-testing-scalatest" % "" % Test
```Published for Scala 3.1+, 2.13, 2.12, as well as ScalaJS 1.7.x. Depends on Cats Effect 3.1+ and scalatest 3.2.6.
Early versions (`0.x.y`) were published under the `com.codecommit` groupId.
## µTest
```scala
import scala.concurrent.duration._
import utest._
import cats.syntax.all._
import cats.effect.IO
import cats.effect.testing.utest.IOTestSuiteobject SimpleSuite extends IOTestSuite {
override val timeout = 1.second // Default timeout is 10 secondsval tests = Tests {
test("do the thing") {
IO(assert(true))
}
}
}```
### Usage
```sbt
libraryDependencies += "org.typelevel" %% "cats-effect-testing-utest" % "" % Test
```Published for Scala 3.1+, 2.13, 2.12, as well as ScalaJS 1.7.x. Depends on Cats Effect 3.1+ and µTest 0.7.9.
Early versions (`0.x.y`) were published under the `com.codecommit` groupId.
## Minitest
Minitest is very similar to uTest, but being strongly typed, there's no need to support
non-IO tests```scala
import scala.concurrent.duration._
import cats.syntax.all._
import cats.effect.IO
import cats.effect.testing.minitest.IOTestSuiteobject SimpleSuite extends IOTestSuite {
override val timeout = 1.second // Default timeout is 10 secondstest("do the thing") {
IO(assert(true))
}
}
```### Usage
```sbt
libraryDependencies += "org.typelevel" %% "cats-effect-testing-minitest" % "" % Test
```Published for Scala 3.1+, 2.13, 2.12, as well as ScalaJS 1.7.x. Depends on Cats Effect 3.1+ and minitest 2.9.5.
Early versions (`0.x.y`) were published under the `com.codecommit` groupId.
## Similar projects
### scalacheck-effect
[scalacheck-effect](https://github.com/typelevel/scalacheck-effect) is a library that extends the functionality of [ScalaCheck](https://scalacheck.org) to support "effectful" properties. An effectful property is one that evaluates each sample in some type constructor `F[_]`.