@@ -7,73 +7,66 @@ disqus: true
7
7
previous-page : /ru/building-a-scala-project-with-intellij-and-sbt
8
8
---
9
9
10
- There are multiple libraries and testing methodologies for Scala,
11
- but in this tutorial, we'll demonstrate one popular option from the ScalaTest framework
12
- called [ FunSuite] ( https://www.scalatest.org/getting_started_with_fun_suite ) .
10
+ Для Scala существует множество библиотек и методологий тестирования,
11
+ но в этом руководстве мы продемонстрируем один популярный вариант из фреймворка ScalaTest
12
+ под названием [ FunSuite] ( https://www.scalatest.org/getting_started_with_fun_suite ) .
13
13
14
- This assumes you know [ how to build a project in IntelliJ] ( building-a-scala-project-with-intellij-and-sbt.html ) .
14
+ Это предполагает, что вы знаете, [ как создать проект в IntelliJ] ( building-a-scala-project-with-intellij-and-sbt.html ) .
15
15
16
- ## Setup
17
- 1 . Create an sbt project in IntelliJ.
18
- 1 . Add the ScalaTest dependency :
19
- 1 . Add the ScalaTest dependency to your ` build.sbt ` file :
16
+ ## Настройка
17
+ 1 . Создайте sbt проект в IntelliJ.
18
+ 1 . Добавьте зависимость ScalaTest:
19
+ 1 . Добавьте зависимость ScalaTest в свой файл ` build.sbt ` :
20
20
```
21
21
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.11" % Test
22
22
```
23
- 1. If you get a notification "build.sbt was changed", select **auto-import**.
24
- 1. These two actions will cause `sbt` to download the ScalaTest library.
25
- 1. Wait for the `sbt` sync to finish; otherwise, `AnyFunSuite` and `test()` will be
26
- unrecognized.
27
- 1. On the project pane on the left, expand `src` => `main`.
28
- 1. Right-click on `scala` and select **New** => **Scala class**.
29
- 1. Call it `CubeCalculator`, change the **Kind** to `object`, and hit enter or double-click on `object`.
30
- 1. Replace the code with the following:
23
+ 1. Если вы получили уведомление "build.sbt was changed", выберите **auto-import**.
24
+ 1. Эти два действия заставят `sbt` подгрузить библиотеки ScalaTest.
25
+ 1. Дождитесь окончания синхронизации `sbt`; в противном случае, `AnyFunSuite` и `test()` не будет распознаны.
26
+ 1. На панели проекта слева разверните `src` => `main`.
27
+ 1. Щелкните правой кнопкой мыши на `scala` и выберите **New** => **Scala class**.
28
+ 1. Назовите новый класс `CubeCalculator`, измените **Kind** на `object`, или дважды щелкните на `object`.
29
+ 1. Вставьте следующий код:
31
30
```
32
- object CubeCalculator extends App {
33
- def cube(x: Int) = {
31
+ object CubeCalculator:
32
+ def cube(x: Int) =
34
33
x * x * x
35
- }
36
- }
37
34
```
38
35
39
- ## Creating a test
40
- 1. On the project pane on the left, expand `src` => `test`.
41
- 1. Right-click on `scala` and select **New** => **Scala class**.
42
- 1. Name the class `CubeCalculatorTest` and hit enter or double-click on `class`.
43
- 1. Replace the code with the following :
36
+ ## Создание теста
37
+ 1. На панели проекта слева разверните `src` => `test`.
38
+ 1. Щелкните правой кнопкой мыши на `scala` и выберите **New** => **Scala class**.
39
+ 1. Назовите новый класс `CubeCalculatorTest` и нажмите **Enter** или дважды щелкните на `class`.
40
+ 1. Вставьте следующий код :
44
41
```
45
42
import org.scalatest.funsuite.AnyFunSuite
46
43
47
- class CubeCalculatorTest extends AnyFunSuite {
44
+ class CubeCalculatorTest extends AnyFunSuite:
48
45
test("CubeCalculator.cube") {
49
46
assert(CubeCalculator.cube(3) === 27)
50
47
}
51
- }
52
48
```
53
- 1. In the source code, right-click `CubeCalculatorTest` and select
49
+ 1. В исходном коде щелкните правой кнопкой мыши на `CubeCalculatorTest` и выберите
54
50
**Run 'CubeCalculatorTest'**.
55
51
56
- ## Understanding the code
52
+ ## Разбор кода
57
53
58
- Let's go over this line by line :
54
+ Давайте разберем код построчно :
59
55
60
- * `class CubeCalculatorTest` means we are testing the object `CubeCalculator`
61
- * `extends AnyFunSuite` lets us use functionality of ScalaTest's AnyFunSuite class
62
- such as the `test` function
63
- * `test` is a function that comes from the FunSuite library that collects
64
- results from assertions within the function body.
65
- * `"CubeCalculator.cube"` is a name for the test. You can call it anything but
66
- one convention is "ClassName.methodName".
67
- * `assert` takes a boolean condition and determines whether the test passes or fails.
68
- * `CubeCalculator.cube(3) === 27` checks whether the output of the `cube` function is
69
- indeed 27. The `===` is part of ScalaTest and provides clean error messages.
56
+ * `class CubeCalculatorTest` означает, что мы тестируем `CubeCalculator`
57
+ * `extends AnyFunSuite` позволяет нам использовать функциональность класса AnyFunSuite из ScalaTest,
58
+ такую как функция `test`
59
+ * `test` это функция из библиотеки FunSuite, которая собирает результаты проверок в теле функции.
60
+ * `"CubeCalculator.cube"` - это имя для теста. Вы можете называть тест как угодно, но по соглашению используется имя — "ClassName.methodName".
61
+ * `assert` принимает логическое условие и определяет, пройден тест или нет.
62
+ * `CubeCalculator.cube(3) === 27` проверяет, действительно ли вывод функции `cube` равен 27.
63
+ `===` является частью ScalaTest и предоставляет понятные сообщения об ошибках.
70
64
71
- ## Adding another test case
72
- 1. Add another `assert` statement after the first one that checks for the cube
73
- of `0`.
74
- 1. Re-run the test again by right-clicking `CubeCalculatorTest` and selecting
75
- 'Run **CubeCalculatorTest**'.
65
+ ## Добавление еще одного теста
66
+ 1. Добавьте еще один оператор `assert` после первого, который проверяет 0 в кубе.
67
+ 1. Перезапустите тест `CubeCalculatorTest`, кликнув правой кнопкой мыши и выбрав
68
+ **Run 'CubeCalculatorTest'**.
76
69
77
- ## Conclusion
78
- You've seen one way to test your Scala code. You can learn more about ScalaTest's
79
- FunSuite on the [official website ](https://www.scalatest.org/getting_started_with_fun_suite).
70
+ ## Резюме
71
+ Вы видели один из способов тестирования Scala кода.
72
+ Узнать больше о FunSuite от ScalaTest можно на [официальном сайте ](https://www.scalatest.org/getting_started_with_fun_suite).
0 commit comments