forked from scala/docs.scala-lang
-
Notifications
You must be signed in to change notification settings - Fork 4
Toolkit: MUnit, os-lib, sttp and upickle tutorials #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Changes from 103 commits
Commits
Show all changes
181 commits
Select commit
Hold shift + click to select a range
d153307
Init tutorials about MUnit in the Scala Toolkit
adpi2 a8dd89e
Tutorial for MUnit async tests.
sjrd 20ea8d2
Small imprvements
adpi2 032fadb
Merge pull request #9 from sjrd/tutorial-munit-async
adpi2 263d988
Add tutorial on writing assertions
julienrf afc5122
+you -we
julienrf 0d96b56
complete tutorial for running individual tests
bishabosha eb3808a
Merge pull request #10 from scalacenter/toolkit-munit-single-test
bishabosha 633bd5b
Improve intro and “next steps”
julienrf b3a659b
Make munit-test-suite tutorial more concise
adpi2 af14a6b
Adding small tutorial about testing exceptions
adpi2 cd85d90
Merge pull request #15 from adpi2/munit-exceptions
adpi2 23521c6
Remove explanations
julienrf 0378714
Merge pull request #11 from scalacenter/assertions
adpi2 083c32d
Init tutorials about sttp
szymon-rd cc77932
Fix munit -> sttp
szymon-rd fdc3e18
Navigate from munit
szymon-rd b89bead
Fix next page on sttp-request-body
szymon-rd 3ac518e
Tutorial on uploading files
szymon-rd a7074af
Fixing some typos
szymon-rd b206115
Apply suggestions from code review
szymon-rd ffb9c8a
Rename dynamic urls tutorialg
szymon-rd 22b404b
Fix address & change the link
szymon-rd d171760
Split into lines
szymon-rd 8ec129f
Fix some URLs
szymon-rd f3f84bd
Add tutorial about flaky tests
adpi2 e36d7de
Merge pull request #17 from adpi2/munit-flakiness
adpi2 19518b4
Add tutorial about resource management in MUnit
adpi2 8e00657
Merge pull request #16 from adpi2/munit-resources
adpi2 9fe2e2f
Review & structure fixes to sttp tutorials
szymon-rd 900bbdd
Missing .html
szymon-rd d34f2ae
Fix links
szymon-rd 3856742
Shorter sentences and other changes
adpi2 67e1af0
Apply suggestions from code review
adpi2 a1d7f4e
WIP: os-lib tutorial
SethTisue 68e9c3f
wip
SethTisue ad9a25c
wip
SethTisue 4b385c8
wip
SethTisue 03746e8
wip
SethTisue 5850414
wip
SethTisue 45dcb3b
wip
SethTisue 3ab3445
wip
SethTisue 4b1f464
apply review feedback from Szymon
SethTisue af3bb0e
Reworking tutorials to have two tabs
szymon-rd 3aa5519
Add scala versions to snippets
szymon-rd e85098c
Fix Scala-cli installation
szymon-rd 0a4d1cd
Merge remote-tracking branch 'origin/main' into toolkit
adpi2 e8a1625
Explain testOnly
adpi2 be04161
Merge pull request #18 from scalacenter/toolkit-sttp
adpi2 4896542
Init upicle tutorials
szymon-rd d9bc226
Reading jsons lesson
szymon-rd 3df21f8
Fixes to the tutorial
szymon-rd 34c94b7
More work on reading JSONs tutorial
szymon-rd bf80ca8
3 tutorials in total for upickle
szymon-rd 484981f
More tutorials on upickle
szymon-rd 4a2ccf6
Update links to prev/next
szymon-rd 7fadcaa
Fixes in the upickle tutorials
szymon-rd 3862453
Fix links
szymon-rd 6a19bdc
Review JSON tutorials
adpi2 f9d9678
Add scala versions to snippets
szymon-rd d4a229b
Merge pull request #22 from scalacenter/toolkit-upickle
adpi2 6aacbd1
Merge remote-tracking branch 'scalacenter/toolkit' into oslib-tutorial
SethTisue f3865fe
Tutorials on STTP x UPickle
szymon-rd 07e2c63
Use real examples from the Github REST API
adpi2 17bb590
Add snippets for different versions of Scala
szymon-rd 8129767
Merge pull request #23 from scalacenter/toolkit-sttp-upickle
adpi2 5cc1259
Merge pull request #21 from SethTisue/oslib-tutorial
adpi2 b16b40c
Fix pages of toolkit tutorials
adpi2 7deb40c
Restructuring the Toolkit tutorials
adpi2 c1efe88
Fix title of upickle-parse-json
adpi2 03a61e8
Fix titles of upickle-parse-json and sttp-query-parameters
adpi2 8dad30c
OS-Lib adjustments
SethTisue 8e2d901
what else can OS-Lib do
SethTisue cb8d0ce
oslib: take Julien's suggestion
SethTisue 20318c9
add code tabs in _zh-cn/overviews/scala3-book/methods-most.md (#2735)
benluo f445e8d
Define the SIP Committee Secretary role (#2759)
julienrf 82bc07c
add code tabs _zh-cn/overviews/scala3-book/methods-main-methods.md
benluo 4d36216
add code tab in _zh-cn/overviews/scala3-book/fp-functional-error-hand…
benluo ab2f720
add code tab in _zh-cn/overviews/scala3-book/types-introduction.md
benluo 8858474
add code tab in _zh-cn/overviews/scala3-book/fp-functions-are-values.md
benluo 46beedb
add code tab in _zh-cn/overviews/scala3-book/fp-immutable-values.md
benluo 52bd30d
add code tab in _zh-cn/overviews/scala3-book/collections-methods.md
benluo 985834d
add code tab _zh-cn/overviews/scala3-book/fun-write-method-returns-fu…
benluo 6f61983
corrected.
benluo 60c2d30
add code tab _zh-cn/overviews/scala3-book/fun-write-map-function.md
benluo 3ee9257
add code tab in _zh-cn/overviews/scala3-book/fun-eta-expansion.md
benluo 5fc921b
add code tab in _zh-cn/overviews/scala3-book/fun-function-variables.md
benluo 1eb3108
add code tab in _zh-cn/overviews/scala3-book/fun-anonymous-functions.md
benluo 864d1a5
Add Scala 2 version of case classes that can evolve (#2760)
julienrf 36b8244
add code tab in types-variance.md (#2756)
benluo c3fd0a3
add code tab in _zh-cn/overviews/scala3-book/fun-hofs.md (#2742)
benluo 3129013
add code tab in _zh-cn/overviews/scala3-book/fp-pure-functions.md
benluo 33b9f86
Add explanation of, and link to TASTyViz (#2761)
Sporarum fd56889
Add code tabs to contextual abstractions page (#2724)
Dedelweiss 2be10cd
add code tab in five files. (#2753)
benluo 5c5ef70
add code tab in packaging-imports.md (#2745)
benluo 1ca93bf
revise OS-Lib examples to not be REPL based
SethTisue 9af67c6
Add short introduction
adpi2 28a9fc9
Add MUnit intro
adpi2 16b26fc
Add uPickle intro
adpi2 7c02349
Add uPickle what else
adpi2 38e7d66
Add a word about why testing is useful
adpi2 43d18f3
expand overall intro
SethTisue bfba2d4
expand introduction
SethTisue a1e7332
Add sttp intro
adpi2 9c15377
Update package-objects.md
dtv27 6221dac
Merge pull request #2764 from dtv27/patch-1
Philippus c2ac051
Link build tool tab together
adpi2 4f40de7
Add checks for class=tabs-build-tool
adpi2 931ecb4
Fix images in debugging compiler guide (#2763)
adpi2 66a5a62
Update 2023-03-17-meeting.md
julienrf a5138b7
Fix function.js
adpi2 aa21d52
fix default build tool tab
adpi2 6e286c4
Bump nokogiri from 1.14.2 to 1.14.3
dependabot[bot] b068441
Bump commonmarker from 0.23.8 to 0.23.9
dependabot[bot] 05f9967
add code tab in _zh-cn/overviews/scala3-book/collections-classes.md (…
benluo b26b7c5
fix typo in `start-intro.md`
d66d766
Add links to debugger in inspection article (#2762)
Sporarum 5a43175
Rework introduction
adpi2 d9d3bb1
update Chinese translation according to latest English version (#2678)
benluo ae8cd40
Review MUnit
adpi2 09bc3aa
Review install boxes in intros
adpi2 956bc08
Review OS-Lib
adpi2 86fe255
Update SIPs state
scala-improvement-bot 0cb1c34
Add more guidelines for labelling code examples for Scala 2/3 (#2768)
julienrf 83db85c
Review upickle
adpi2 c9c9716
Review sttp
adpi2 da879bb
Update nums and pages
adpi2 5c1ac17
Finishing touches
adpi2 d42a8db
Ambiguity in Tour of Scala : Case Classes. issue #2771
4ea8301
Mention specialized
som-snytt 733f081
Fix wording error (#2770)
matthew-hagemann 5fa024b
Update SIPs state
scala-improvement-bot 012cf86
Add a mention to the provided Liquid variables
JD557 be270a1
Make buf final in string interpolation docs
andrzejressel 69420dd
Update SIPs state
scala-improvement-bot 122bec2
Update SIPs state
scala-improvement-bot 082440b
Add results of SIP meeting of 21st of April 2023
julienrf bfc2326
Update SIPs state
scala-improvement-bot 78edb5e
update Gemfile.lock for MacOS/Darwin
SethTisue 9566e2c
Merge pull request #2776 from andrzejressel/patch-1
julienrf 05a1ae4
Add "Functional programming" section in russian. (#2651)
artemkorsakov af8b894
Update SIPs state
scala-improvement-bot 4f33296
Update variances.md due to the language rules (#2730)
DespiteDeath 521e91b
update Gemfile.lock for Windows 10, x64
artemkorsakov 17a1915
Refresh tour/basic.md (#2780)
artemkorsakov 50f2e25
update Gemfile.lock for use on MacOS
SethTisue 30ebba1
make overall intro a bit more concise
SethTisue a75e5ce
proofread MUnit sections
SethTisue 60dc689
proofread uPickle sections
SethTisue 9f39d90
rewrite the sttp intro pretty heavily
SethTisue 93ee020
proofread entire sttp tutorial
SethTisue 7ab0d4f
avoid talk of 'install'ing libraries
SethTisue 2027f27
fix broken link
SethTisue 8b60216
Add types-introduction.md in russian
artemkorsakov 4c6a3c7
Refresh _ru\tour\named-argument.md (#2784)
artemkorsakov 0b6ecb9
Refresh /tour/unified-types.md (#2781)
artemkorsakov a2f2239
Add example for error handling in ExecutionContext (#2777)
som-snytt 9ea05b2
Merge pull request #2786 from artemkorsakov/Add_types-introduction_in_ru
sjrd 441d8a9
Refresh _ru/tour/default-parameter-values.md (#2783)
artemkorsakov 6a07424
Refresh _ru/tour/classes.md (#2782)
artemkorsakov 79a4925
Update versions
adpi2 00f9511
small fixes in munit
adpi2 271bcb8
small fixes in uPickle
adpi2 f76106b
Rename files
adpi2 ae6e397
Run mdoc in http-client tutos
adpi2 680d2fc
Run mdoc on JSON tutorials
adpi2 5087957
Run mdoc in OS tutorials
adpi2 ef2e3c3
run mdoc in testing tutorials
adpi2 ba1a04d
fix mdoc
adpi2 850ec10
using dep, not using lib
SethTisue 9492c50
Refresh _ru\tour\traits.md (#2790)
artemkorsakov 3c120bd
Add types-inferred.md and types-generics.md in russian (#2789)
artemkorsakov c53b084
Add toolkit in the index page and nav bar
adpi2 c0d9150
Use toolkit lib in all build tools
adpi2 ee6ffc9
Add index in intro
adpi2 a587ace
Use dep toolkit-test
adpi2 ed1932c
Fix install munit
adpi2 3d6e5b7
Update SIPs state
scala-improvement-bot 8d227be
Add code tabs to Contextual Abstractions (#2787)
julienrf e51bd07
Merge remote-tracking branch 'origin/main' into toolkit
adpi2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{% altDetails install-info-box 'Getting MUnit' %} | ||
|
||
{% tabs munit-unit-test-1 class=tabs-build-tool %} | ||
{% tab 'Scala CLI' %} | ||
You can require the entire toolkit in a single line: | ||
```scala | ||
//> using toolkit "latest" | ||
``` | ||
|
||
Alternatively, you can require a specific version of MUnit: | ||
```scala | ||
//> using lib "org.scalameta::munit:1.0.0-M6" | ||
``` | ||
{% endtab %} | ||
{% tab 'sbt' %} | ||
In your build.sbt file, you can add the dependency to the MUnit library: | ||
```scala | ||
lazy val example = project.in(file("example")) | ||
.settings( | ||
scalaVersion := "3.2.1", | ||
libraryDependencies += "org.scalameta" %% "munit" % "1.0.0-M6" % Test | ||
) | ||
``` | ||
Here the `Test` configuration means that this dependency is only used by the source files in the `example/src/test` directory. | ||
This is where you can put your test suites. | ||
{% endtab %} | ||
{% tab 'Mill' %} | ||
In your build.sc file, you can add a `test` object extending `Tests` and `TestModule.Munit`: | ||
```scala | ||
object example extends ScalaModule { | ||
def scalaVersion = "3.2.1" | ||
object test extends Tests with TestModule.Munit { | ||
def ivyDeps = | ||
Agg( | ||
ivy"org.scalameta::munit::1.0.0-M6" | ||
) | ||
} | ||
} | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
{% endaltDetails %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{% altDetails require-info-box 'Getting OS-Lib' %} | ||
|
||
{% tabs oslib-install class=tabs-build-tool %} | ||
{% tab 'Scala CLI' %} | ||
You can require the entire toolkit in a single line: | ||
```scala | ||
//> using toolkit "latest" | ||
``` | ||
|
||
Alternatively, you can require just a specific version of OS-Lib: | ||
```scala | ||
//> using dep "com.lihaoyi::os-lib:0.9.1" | ||
adpi2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
{% endtab %} | ||
{% tab 'sbt' %} | ||
In our `build.sbt` file, we add the dependency on OS-Lib: | ||
```scala | ||
libraryDependencies += "com.lihaoyi" %% "os-lib" % "0.9.1" | ||
``` | ||
{% endtab %} | ||
{% tab 'Mill' %} | ||
In our `build.sc` file, we add the dependency on OS-Lib: | ||
```scala | ||
def ivyDeps = Agg( | ||
ivy"com.lihaoyi::os-lib:0.9.1" | ||
) | ||
adpi2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
{% endaltDetails %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{% altDetails install-info-box 'Getting sttp' %} | ||
|
||
{% tabs sttp-install-methods class=tabs-build-tool%} | ||
{% tab 'Scala CLI' %} | ||
You can require the entire toolkit in a single line: | ||
```scala | ||
//> using toolkit "latest" | ||
``` | ||
|
||
Alternatively, you can require a specific version of sttp: | ||
```scala | ||
//> using lib "com.softwaremill.sttp.client4::core:4.0.0-M1" | ||
``` | ||
{% endtab %} | ||
{% tab 'sbt' %} | ||
In your build.sbt file, you can add the dependency to the sttp library: | ||
```scala | ||
lazy val example = project.in(file("example")) | ||
.settings( | ||
scalaVersion := "3.2.1", | ||
libraryDependencies += "com.softwaremill.sttp.client4" %% "core" % "4.0.0-M1" | ||
) | ||
``` | ||
{% endtab %} | ||
{% tab 'Mill' %} | ||
In your build.sc file, you can add the dependency to the sttp library: | ||
```scala | ||
object example extends ScalaModule { | ||
def scalaVersion = "3.2.1" | ||
def ivyDeps = | ||
Agg( | ||
ivy"com.softwaremill.sttp.client4::core:4.0.0-M1" | ||
) | ||
} | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
{% endaltDetails %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{% altDetails install-info-box 'Getting upickle' %} | ||
|
||
{% tabs upickle-install-methods class=tabs-build-tool %} | ||
{% tab 'Scala CLI' %} | ||
Using Scala CLI, you can require the entire toolkit in a single line: | ||
```scala | ||
//> using toolkit "latest" | ||
``` | ||
|
||
Alternatively, you can require a specific version of upickle: | ||
```scala | ||
//> using lib "com.lihaoyi::upickle:1.6.0" | ||
``` | ||
{% endtab %} | ||
{% tab 'sbt' %} | ||
In your build.sbt file, you can add the dependency to the upickle library: | ||
```scala | ||
lazy val example = project.in(file("example")) | ||
.settings( | ||
scalaVersion := "3.2.1", | ||
libraryDependencies += "com.lihaoyi" %% "upickle" % "1.6.0" | ||
) | ||
``` | ||
{% endtab %} | ||
{% tab 'Mill' %} | ||
In your build.sc file, you can add the dependency to the upickle library: | ||
```scala | ||
object example extends ScalaModule { | ||
def scalaVersion = "3.2.1" | ||
def ivyDeps = | ||
Agg( | ||
ivy"com.lihaoyi::upickle:1.6.0" | ||
) | ||
} | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
{% endaltDetails %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{%if include.selector%}<{{include.selector}} {%if include.classes%}class="{{include.classes}}"{%endif%} {%if include.id%}id="{{include.id}}{%endif%}">{%endif%} | ||
{% capture markdown %}{% include {{include.path}} %}{% endcapture %}{{ markdown | markdownify }} | ||
{%if include.selector%}</{{include.selector}}>{%endif%} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
introduction.md | ||
munit-intro.md | ||
munit-tests.md | ||
munit-run.md | ||
munit-run-only.md | ||
munit-exceptions.md | ||
munit-asynchronous-tests.md | ||
munit-resources.md | ||
munit-what-else.md | ||
oslib-intro.md | ||
oslib-read-directory.md | ||
oslib-read-file.md | ||
oslib-write-file.md | ||
oslib-run-process.md | ||
oslib-what-else.md | ||
upickle-intro.md | ||
upickle-parse-json.md | ||
upickle-modify-json.md | ||
upickle-deserialize.md | ||
upickle-serialize.md | ||
upickle-files.md | ||
upickle-what-else.md | ||
sttp-intro.md | ||
sttp-request.md | ||
sttp-uris.md | ||
sttp-request-body.md | ||
sttp-json.md | ||
sttp-upload-file.md | ||
sttp-what-else.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
title: Introduction | ||
type: chapter | ||
description: Introducing the Scala Toolkit tutorials | ||
num: 1 | ||
previous-page: | ||
next-page: munit-intro | ||
--- | ||
|
||
## What is the Scala Toolkit? | ||
|
||
The Scala Toolkit is a set of libraries designed to make common programming tasks simple and efficient. It supports working with files and processes, parsing JSON, sending HTTP requests, and unit testing. | ||
|
||
The Toolkit supports: | ||
* Scripts and applications | ||
* Scala 3 and Scala 2 | ||
* JVM, Scala.js, and Scala Native | ||
|
||
Use cases for the toolkit include: | ||
|
||
- backend scripts that run on JVM, to scrape a website, to collect and transform data, or to fetch and process some files, | ||
- frontend scripts that run on the browser and power your websites, | ||
- command-line scripts | ||
- command-line tools packaged as native binaries for instant startup | ||
|
||
## What are these tutorials? | ||
|
||
This series of tutorials focuses on short code examples, to help you get started quickly. | ||
|
||
If you need more in-depth information, the tutorials include links to further documentation for all of the libraries in the toolkit. | ||
|
||
## How to run the code? | ||
|
||
You can follow the tutorials regardless of how you choose to run your | ||
Scala code. The tutorials focus on the code itself, not on the process | ||
of running it. | ||
|
||
Ways to run Scala code include: | ||
* in your **browser** with [Scastie](https://scastie.scala-lang.org) | ||
* pros: zero installation, online sharing | ||
* cons: single-file only, online-only | ||
* interactively in the Scala **REPL** (Read/Eval/Print Loop) | ||
* pros: interactive exploration in the terminal | ||
* cons: doesn't save your code anywhere | ||
* interactively in a **worksheet** in your IDE such as [IntelliJ](https://www.jetbrains.com/help/idea/discover-intellij-idea-for-scala.html) or [Metals](http://scalameta.org/metals/) | ||
* pros: interactive exploration in a GUI | ||
* cons: requires worksheet environment to run | ||
* in **scripts**, using [Scala CLI](https://scala-cli.virtuslab.com) | ||
* pros: terminal-based workflow with little setup | ||
* cons: may not be suitable for large projects | ||
* using a **build tool** (such as [sbt](https://www.scala-sbt.org) or [mill](https://com-lihaoyi.github.io/mill/)) | ||
* pros: terminal-based workflow for projects of any size | ||
* cons: requires some additional setup and learning | ||
* using an **IDE** such as [IntelliJ](https://www.jetbrains.com/help/idea/discover-intellij-idea-for-scala.html) or [Metals](http://scalameta.org/metals/) | ||
* pros: GUI based workflow for projects of any size | ||
* cons: requires some additional setup and learning | ||
|
||
These choices, with their pros and cons, are common to most programing | ||
languages. | ||
Feel free to use whichever option you're most comfortable with. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
--- | ||
title: How to write asynchronous tests? | ||
type: section | ||
description: Writing asynchronous tests using MUnit | ||
num: 7 | ||
previous-page: munit-exceptions | ||
next-page: munit-resources | ||
--- | ||
|
||
{% include markdown.html path="_markdown/install-munit.md" %} | ||
|
||
## Asynchronous tests | ||
|
||
In Scala, it's common for an *asynchronous* method to return a `Future`. | ||
MUnit offers special support for `Future`s. | ||
|
||
For example, consider an asynchronous variant of a `square` method: | ||
|
||
{% tabs 'async-1' class=tabs-scala-version %} | ||
{% tab 'Scala 2' %} | ||
```scala | ||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
object AsyncMathLib { | ||
def square(x: Int)(using ExecutionContext): Future[Int] = | ||
Future(x * x) | ||
} | ||
``` | ||
{% endtab %} | ||
{% tab 'Scala 3' %} | ||
```scala | ||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
object AsyncMathLib: | ||
def square(x: Int)(implicit ec: ExecutionContext): Future[Int] = | ||
Future(x * x) | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
A test itself can return a `Future[Unit]`. | ||
MUnit will wait behind the scenes for the resulting `Future` to complete, failing the test if any assertion fails. | ||
|
||
You can therefore write the test as follows: | ||
|
||
{% tabs 'async-3' class=tabs-scala-version %} | ||
{% tab 'Scala 2' %} | ||
```scala | ||
// Import the global execution context, required to call async methods | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
class AsyncMathLibTests extends munit.FunSuite: | ||
test("square") { | ||
for | ||
squareOf3 <- AsyncMathLib.square(3) | ||
squareOfMinus4 <- AsyncMathLib.square(-4) | ||
yield | ||
assertEquals(squareOf3, 9) | ||
assertEquals(squareOfMinus4, 16) | ||
} | ||
``` | ||
{% endtab %} | ||
{% tab 'Scala 3' %} | ||
```scala | ||
// Import the global execution context, required to call async methods | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
class AsyncMathLibTests extends munit.FunSuite: | ||
test("square"): | ||
for | ||
squareOf3 <- AsyncMathLib.square(3) | ||
squareOfMinus4 <- AsyncMathLib.square(-4) | ||
yield | ||
assertEquals(squareOf3, 9) | ||
assertEquals(squareOfMinus4, 16) | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
The test first asynchronously computes `square(3)` and `square(-4)`. | ||
Once both computations are completed, and if they are both successful, it proceeds with the calls to `assertEquals`. | ||
If any of the assertion fails, the resulting `Future[Unit]` fails, and MUnit will cause the test to fail. | ||
|
||
You may read more about asynchronous tests [in the MUnit documentation](https://scalameta.org/munit/docs/tests.html#declare-async-test). | ||
It shows how to use other asynchronous types besides `Future`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.