All Projects → tkawachi → Sbt Doctest

tkawachi / Sbt Doctest

Doctest for scala

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to Sbt Doctest

Stryker4s
Mutation testing for Scala. Work in progress...
Stars: ✭ 118 (-30.99%)
Mutual labels:  sbt, unit-testing
sbt-example
Run Scaladoc as unit tests
Stars: ✭ 30 (-82.46%)
Mutual labels:  unit-testing, sbt
Dagger2
Kotlin Dagger2 example project
Stars: ✭ 145 (-15.2%)
Mutual labels:  unit-testing
Sbt Site
Site generation for sbt
Stars: ✭ 166 (-2.92%)
Mutual labels:  sbt
Pat
Prometheus Alert Testing utility
Stars: ✭ 156 (-8.77%)
Mutual labels:  unit-testing
Testbook
🧪 📗 Unit test your Jupyter Notebooks the right way
Stars: ✭ 146 (-14.62%)
Mutual labels:  unit-testing
Abot
Cross Platform C# web crawler framework built for speed and flexibility. Please star this project! +1.
Stars: ✭ 1,961 (+1046.78%)
Mutual labels:  unit-testing
Kotlinmvparchitecture
Clean MVP Architecture with Dagger2 + Retrofit2 + Mockito + Fresco + EasiestGenericRecyclerAdapter using Kotlin. Added Unit Tests(Kotlin Tests)!
Stars: ✭ 143 (-16.37%)
Mutual labels:  unit-testing
Snap Shot
Jest-like snapshot feature for the rest of us, works magically by finding the right caller function
Stars: ✭ 170 (-0.58%)
Mutual labels:  unit-testing
Systemwrapper
.NET library for easier testing of system APIs.
Stars: ✭ 153 (-10.53%)
Mutual labels:  unit-testing
Vstest Docs
Documentation for the Visual Studio Test Platform.
Stars: ✭ 165 (-3.51%)
Mutual labels:  unit-testing
Scala Ts
🔧 Scala to TypeScript code generator
Stars: ✭ 152 (-11.11%)
Mutual labels:  sbt
Bookstore Ios
 Sample iOS App - A collection of examples and patterns for Unit Testing, UI Testing, handling Result/Optionals, writing documentation, and more. Details in README.
Stars: ✭ 147 (-14.04%)
Mutual labels:  unit-testing
Sample Projects
Sample project files for JavaCPP, JavaCPP Presets, and JavaCV
Stars: ✭ 160 (-6.43%)
Mutual labels:  sbt
Supersafebank
Sample Event Sourcing implementation with .NET Core
Stars: ✭ 142 (-16.96%)
Mutual labels:  unit-testing
Cckit
Programming toolkit for building Hyperledger Fabric Golang on-chain (chaincode) and off-chain applications
Stars: ✭ 167 (-2.34%)
Mutual labels:  unit-testing
Dockertest
Write better integration tests! Dockertest helps you boot up ephermal docker images for your Go tests with minimal work.
Stars: ✭ 2,254 (+1218.13%)
Mutual labels:  unit-testing
Android Client
An android client for the MifosX platform
Stars: ✭ 150 (-12.28%)
Mutual labels:  unit-testing
Cypress Example Recipes
Various recipes for testing common scenarios with Cypress
Stars: ✭ 2,485 (+1353.22%)
Mutual labels:  unit-testing
Jitpack.io
Documentation and issues of https://jitpack.io
Stars: ✭ 2,156 (+1160.82%)
Mutual labels:  sbt

sbt-doctest

Plugin for sbt that generates tests from examples in ScalaDoc.

Codacy Badge

Install

To use this plugin, add it to your project/plugins.sbt.

addSbtPlugin("com.github.tkawachi" % "sbt-doctest" % "0.9.9")

This plugin supports sbt 1.x.

It's automatically enabled for JVM projects. Scala.js is currently not supported (See #52).

sbt-doctest allows you to choose which test library to use by doctestTestFramework. By default, the tests are generated for ScalaCheck. The test libraries need to be added separately to libraryDependencies.

Using ScalaCheck

If you are using ScalaCheck, add the following lines to your build.sbt:

libraryDependencies ++= Seq(
  "org.scalacheck" %% "scalacheck" % "1.15.2" % Test
)

doctestTestFramework := DoctestTestFramework.ScalaCheck // Default value for doctestTestFramework

Using ScalaTest

If you are using ScalaTest, add the following lines to your build.sbt:

// ScalaTest 3.2
// ScalaTest 3.2 has been modularized. sbt-doctest generates tests using FunSpec.
libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest-funspec" % "3.2.3" % Test,
  "org.scalatestplus" %% "scalacheck-1-15" % "3.2.3.0" % Test
)

// ScalaTest 3.1
libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest" % "3.1.2" % Test,
  "org.scalatestplus" %% "scalacheck-1-14" % "3.1.2.0" % Test
)

// ScalaTest 3.0
libraryDependencies ++= Seq(
  "org.scalatest"  %% "scalatest"  % "3.0.9"  % Test,
  "org.scalacheck" %% "scalacheck" % "1.14.0" % Test
)

doctestTestFramework := DoctestTestFramework.ScalaTest

Due to changes in the ScalaTest API, the test code generated will be slightly different depending on the version of ScalaTest. sbt-doctest automatically determines which test code to generate by looking at libraryDependencies.

If you want to explicitly specify the version of ScalaTest to be generated, you can specify doctestScalaTestVersion.

doctestScalaTestVersion := Some("3.2.3")

Using Specs2

If you are using Specs2, add the following lines to your build.sbt:

libraryDependencies ++= Seq(
  "org.specs2" %% "specs2-core"       % "4.10.5" % Test,
  "org.specs2" %% "specs2-scalacheck" % "4.10.5" % Test
)

doctestTestFramework := DoctestTestFramework.Specs2

Using Minitest

If you are using Minitest, add the following lines to your build.sbt:

libraryDependencies ++= Seq(
  "io.monix" %% "minitest" % "2.8.2" % Test,
  "io.monix" %% "minitest-laws" % "2.8.2" % Test
)

doctestTestFramework := DoctestTestFramework.Minitest

Using µTest

If you are using µTest, add the following lines to your build.sbt:

libraryDependencies ++= Seq(
  "com.lihaoyi" %% "utest" % "0.6.4" % Test
)

doctestTestFramework := DoctestTestFramework.MicroTest

Using MUnit

If you are using MUnit, add the following lines to your build.sbt:

libraryDependencies ++= Seq(
  "org.scalameta" %% "munit" % "0.7.20" % Test
)

doctestTestFramework := DoctestTestFramework.Munit
testFrameworks += new TestFramework("munit.Framework")

In case you are configuring µTest or using a custom test framework, you can do something like this below in your build.sbt:

testFrameworks -= new TestFramework("utest.runner.Framework")
testFrameworks += new TestFramework("test.utest.MyCustomFramework")

which means that you are removing utest.runner.Framework inserted automatically for you by sbt-doctest and you are inserting your own custom test framework, provided by class test.utest.MyCustomFramework, in this example.

Caveats

There are still dependencies from ScalaTest and/or ScalaCheck when property checks are employed.

The difficulty can be circumvented for the time being by providing all dependencies in build.sbt, like shown in the example below which uses uTest with property checks, which require ScalaTest and ScalaCheck as well:

libraryDependencies ++= Seq(
  "com.lihaoyi"    %% "utest"      % "0.6.4"  % Test,
  "org.scalatest"  %% "scalatest"  % "3.0.9"  % Test,
  "org.scalacheck" %% "scalacheck" % "1.15.2" % Test
)
      
doctestTestFramework := DoctestTestFramework.MicroTest

Usage

sbt-doctest will generate tests from doctests in ScalaDoc comments. These tests are automatically generated and run when sbt's test task is invoked.

Here is an example that shows the different doctest styles that are supported by the plugin:

object Test {

  /**
   * A sample function.
   *
   * {{{
   * # Python style
   * >>> Test.f(10)
   * 20
   *
   * # Scala REPL style
   * scala> Test.f(20)
   * res1: Int = 40
   *
   * # Property based test
   * prop> (i: Int) => Test.f(i) == (i * 2)
   * }}}
   */
  def f(x: Int) = x + x
}

It also supports multi-line inputs:

/**
 * {{{
 * # Python style
 * >>> Test.f(
 * ...   10
 * ... )
 * 20
 *
 * # Scala REPL style
 * scala> Test.f(
 *      |   20
 *      | )
 * res1: Int = 40
 *
 * # Property based test
 * prop> (i: Int) =>
 *     |   Test.f(i) == (i * 2)
 * }}}
 */
def f(x: Int) = x + x

Please use <BLANKLINE> when an output contains blank lines.

/**
 * {{{
 * # Python style
 * >>> Test.helloWorld
 * Hello
 * <BLANKLINE>
 * World
 *
 * # Scala REPL style
 * scala> Test.helloWorld
 * res0: String =
 * Hello
 * <BLANKLINE>
 * World
 * }}}
 */
def helloWorld = "Hello\n\nWorld"

Ignoring Some Files

If you don't want to generate doctests for some of your sources, then specify a regex pattern:

doctestIgnoreRegex := Some(".*SomeClass.scala")

Source files with canonical paths (using UNIX separator - /) matching the regex, will not be used for doctest generation.

Only Code Blocks Mode

If you all you need is to check that code in Scaladoc code blocks (text inside {{{}}}) compiles), you can enable only code blocks mode by setting doctestOnlyCodeBlocksMode to true:

doctestOnlyCodeBlocksMode := true

Generated tests won't have any assertions, unless they are present in your Scaladocs.

HTML Entities

Often when documenting libraries that work with HTML you need to encode HTML entities so that they will be displayed in browsers.

However, sbt-doctest ignores these and attempts to compare encoded HTML with unencoded HTML entities. You can fix this by enabling decoding of HTML entities. Just add the following setting to your build.sbt:

doctestDecodeHtmlEntities := true

Now the following should pass:

  /**
   * {{{
   * >>> Main.html
   * &lt;html&gt;&lt;/html&gt;
   * }}}
   */
  val html = "<html></html>"

Markdown

Also supports code examples in Markdown documentation. To enable add the following to your build.sbt:

doctestMarkdownEnabled := true

Any code blocks that start with the ```scala markdown directive will be parsed. It searches *.md under baseDirectory by default. It can be configured by doctestMarkdownPathFinder.

// default
doctestMarkdownPathFinder := baseDirectory.value * "*.md"

// search doc/ recursively
doctestMarkdownPathFinder := baseDirectory.value * "doc" ** "*.md" 

See an example markdown.

Compatibility with other sbt plugins

If you happen to have other plugins that use scalameta (e.g. sbt-scalafmt) please make sure those plugins don't bring conflicting version of scalameta.

At this moment sbt-scalafmt need to be of version 1.6.x at least.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].