官术网_书友最值得收藏!

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.

  1. In the command-line environment, navigate to the root of the project and select the following test:
      Lesson 1/2-project
  2. If you are using a Unix/Linux machine and your code is situated in courses/pactscala of your home directory, then run the following command:
     > cd  ~/courses/packscala/Lesson 1/2-project
  3. Run the following command:
        > sbt test
  4. 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.

  1. Open the project in the IDE.
  2. Navigate to Run/Edit Configurations:
    Adding a Test to Our Project
  3. Choose sbt test as the configuration.
    • Check the checkbox Use sbt:
    Adding a Test to Our Project
  4. Select Run sbt-test.
主站蜘蛛池模板: 青海省| 长丰县| 陆河县| 张家港市| 吴江市| 奈曼旗| 彭水| 中江县| 忻城县| 漠河县| 乐昌市| 博湖县| 浠水县| 揭东县| 南平市| 遂宁市| 西和县| 探索| 阳原县| 托克托县| 新巴尔虎左旗| 托克逊县| 二手房| 四川省| 龙泉市| 衡水市| 景洪市| 桐城市| 雷山县| 铁岭县| 利辛县| 普格县| 南皮县| 铁岭市| 漳州市| 信宜市| 旺苍县| 澜沧| 牡丹江市| 昌邑市| 竹山县|