- Professional Scala
- Mads Hartmann Ruslan Shevchenko
- 274字
- 2021-07-23 17:24:24
Unit Testing
In any program which is bigger than arithmetic operations, programmers should make themselves comfortable when it is possible to ensure that new changes are not breaking old functionalities.
The most common technique for this is unit testing, which is where the programmer tests the functionality of the code in parallel with its development by creating a test code which will verify that the code really satisfies their requirements.
The theme of this section will be introducing tools for unit testing in Scala.
Adding a Test to Our Project
Let's add tests to our small program. We'll import <for-students/lesson1/2-project>
in our IDE.
This is the directory schema of a Scala project. For adding tests, we should do the following:
- Add test dependencies to
build.sbt
- Write tests in the source test directory
For adding dependency, let's add the following line to our build.sbt
:
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.4" % "test"
It's an expression in Scala DSL (domain-specific language), which means that we should add scalatest
to our set of library dependencies. Operators %%
and %
are used for forming the name and classifier for published artifacts. You can refer to the sb
t
documentation for more detail: http://www.scala-sbt.org/1.x/docs/Library-Dependencies.html.
Before compilation, sbt
will download scalatest
from a publicly available repository (Maven central), and when running tests, it will add scalatest
to the classpath.
We will now run sbt
tests from the command line.
- In the command-line environment, navigate to the root of the project and select the following test:
Lesson 1/2-project
- If you are using a Unix/Linux machine and your code is situated in
courses/pactscala
of your homedirectory
, then run the following command:> cd ~/courses/packscala/Lesson 1/2-project
- Run the following command
:
> sbt test
- You will get the expected output, which will include the following strings:
[info] ExampleSpec: [info] - example test should pass [info] StepTest: [info] - step of unparded word must be interesting
We will now see how to run sbt
tests from IDEA IDE.
We'll now run sbt Tests from IDEA IDE.
- 連接未來:從古登堡到谷歌的網(wǎng)絡(luò)革命
- Application Development with Qt Creator(Second Edition)
- Building Django 2.0 Web Applications
- 5G承載網(wǎng)網(wǎng)絡(luò)規(guī)劃與組網(wǎng)設(shè)計
- 信息通信網(wǎng)絡(luò)建設(shè)安全管理概要2
- 計算機網(wǎng)絡(luò)工程實用教程(第2版)
- Wireshark網(wǎng)絡(luò)分析就這么簡單
- 網(wǎng)絡(luò)環(huán)境中基于用戶視角的信息質(zhì)量評價研究
- Working with Legacy Systems
- 端到端QoS網(wǎng)絡(luò)設(shè)計
- Learning Storm
- Selenium WebDriver 3 Practical Guide
- Getting Started with tmux
- 小型局域網(wǎng)組建
- 世界互聯(lián)網(wǎng)發(fā)展報告2021