Testing
任何软件开发(尤其是区块链开发)的一个关键部分是测试。在这里,我们将介绍 Move 测试的基础知识以及如何为 Move 代码编写和组织测试。
A crucial part of any software development, and even more - blockchain development, is testing. Here, we will cover the basics of testing in Move and how to write and organize tests for your Move code.
The #[test]
attribute
Move 中的测试是用 #[test] 属性标记的函数。该属性告诉编译器该函数是一个测试函数,并且应该在执行测试时运行它。测试函数是常规函数,但它们不能带参数并且没有返回值。它们被排除在字节码之外,并且永远不会发布。
Tests in Move are functions marked with the #[test]
attribute. This attribute tells the compiler
that the function is a test function, and it should be run when the tests are executed. Test
functions are regular functions, but they must take no arguments and have no return value. They
are excluded from the bytecode, and are never published.
module book::testing {
// Test attribute is placed before the `fun` keyword. Can be both above or
// right before the `fun` keyword: `#[test] fun my_test() { ... }`
// The name of the test would be `book::testing::simple_test`.
#[test]
fun simple_test() {
let sum = 2 + 2;
assert!(sum == 4, 1);
}
// The name of the test would be `book::testing::more_advanced_test`.
#[test] fun more_advanced_test() {
let sum = 2 + 2 + 2;
assert!(sum == 4, 1);
}
}
Running Tests
要运行测试,您可以使用 rooch move test 命令。此命令将首先在测试模式下构建包,然后运行包中找到的所有测试。在测试模式期间,将处理sources/ 和tests/ 目录中的模块,并执行测试。
To run tests, you can use the rooch move test
command. This command will first build the package in
the test mode and then run all the tests found in the package. During test mode, modules from both
sources/
and tests/
directories are processed, and the tests are executed.
$ rooch move test
INCLUDING DEPENDENCY MoveStdlib
INCLUDING DEPENDENCY MoveosStdlib
INCLUDING DEPENDENCY RoochFramework
BUILDING quick_start_object_counter
Running Move unit tests
...