Sway编程语言-更新中(The Sway Programming Language-Updatin
  • Sway编程语言(The Sway Programming Language)
  • 1. 导言(Introduction)
    • 1.1 安装(Installation)
    • 1.2 Sway快速入门 (Sway Quickstart)
    • 1.3 Fuel工具链 (The Fuel Toolchain)
    • 1.4 一个Forc项目 (A Forc Project)
    • 1.5 标准库 (Standard Library)
  • 2. 示例(Example)
    • 2.1计数器(Counter)
    • 2.2子货币(Subcurrency)
    • 2.3 FizzBuzz
    • 2.4 钱包智能合约(Wallet Smart Contract)
  • 3.Sway编程类型(Sway Program Types)
    • 3.1 合约(Contracts)
    • 3.2 库 (Libraries)
    • 3.3 脚本(Scripts)
    • 3.4 谓词 (Predicates)
  • 4. Sway语言基础 (Sway Language basics)
    • 4.1 变量 (Variables)
    • 4.2 内置类型(Built-in Types)
    • 4.3 常用库类型(Commonly Used Library Types)
    • 4.4 区块链类 (Blockchain Types)
    • 4.5 函数 (Functions)
    • 4.6 结构、元祖和穷举 (Structs, Tuples, and Enums)
    • 4.7 方法和关联函数 (Methods and Associated Functions)
    • 4.8 注释和日志 (Comments and Logging)
    • 4.9 控制流 (Control Flow)
  • 5. 用Sway部署区块链 (Blockchain Development with Sway)
    • 5.1 哈希和加密学 (Hashing and Cryptography)
    • 5.2 合约存储(Contract Storage)
    • 5.3 函数纯度 (Function Purity)
    • 5.4 标识符(Identifiers)
    • 5.5 原生资产(Native Assets)
    • 5.6 访问控制 (Access Control)
    • 5.7 调用合约(Calling Contracts)
  • 6. 高级概念 (Advanced Concepts)
    • 6.1 高级类型 (Advanced Types)
    • 6.2 通用类型 (Generic Types)
    • 6.3 特征 (Traits)
    • 6.4 集 (Assembly)
  • 7. 一般集聚 (Common Collections)
    • 7.1 堆上的向量(Vectors on the Heap)
    • 7.2 存储向量 (Storage Vectors)
    • 7.3 存储映射 (Storage Maps)
  • 8.测试(Testing)
    • 8.1 单元测试(Unit Testing)
    • 8.2 用Rust来测试 (Testing with Rust)
  • 9.应用前端开发 (Application Frontend Development)
    • 9.1 TypeScript SDK
  • 10.Sway应用(Sway Reference)
    • 10.1 编译器内部函数(Compiler Intrinsics)
    • 10.2 属性(Attributes)
    • 10.3 风格向导(Style Guide)
    • 10.4 已知各类问题(Known Issues and Workarounds)
    • 10.5 与Solidity的不同之处 (Differences From Solidity)
    • 10.6 与Rust的不同之处 (Differences From Rust)
    • 10.7 向Sway贡献 (Contributing To Sway)
  • 11. Forc引用 (Forc Reference)
    • 11.1清单参考 (Manifest Reference)
    • 11.2 工作区(Workspaces)
    • 11.3 依赖(Dependencies)
    • 11.4 命令(Commands)
      • 11.4.1 forc-addr2line
      • 11.4.2 forc-build
      • 11.4.3 forc-check
Powered by GitBook
On this page
  • 构建和运行测试(Building and Running Tests)
  • 测试失败(Testing Failure )
  • 调用合约(Calling Contracts)
  • 并行或串行运行测试 (Running Tests in Parallel or Serially)
  1. 8.测试(Testing)

8.1 单元测试(Unit Testing)

Previous8.测试(Testing)Next8.2 用Rust来测试 (Testing with Rust)

Last updated 1 year ago

Forc provides built-in support for building and executing tests for a package.

Forc 为包的构建和执行测试,提供内置支持。

Tests are written as free functions with the #[test] attribute. For example:

测试被编写为带有#[test] 属性的自由函数。例如:

#[test]
fn test_meaning_of_life() {
    assert(6 * 7 == 42);
}

Each test function is ran as if it were the entry point for a . Tests "pass" if they return successfully, and "fail" if they revert or vice versa while .

每个测试函数的运行就好像它是一个 的入点一样。如果成功返回则测试“通过”,否则返回“失败”,在 时也一样。

If the project has failing tests forc test will exit with exit status 101.

如果项目的测试失败,forc test 将以状态 101 退出。

构建和运行测试(Building and Running Tests)

We can build and execute all tests within a package with the following:

我们可以使用以下命令在一个包中构建和执行所有测试:

forc test

The output should look similar to this:

输出应与此类似:

  Compiled library "core".
  Compiled library "std".
  Compiled library "lib_single_test".
  Bytecode size is 92 bytes.
   Running 1 tests
      test test_meaning_of_life ... ok (170.652µs)
   Result: OK. 1 passed. 0 failed. Finished in 1.564996ms.

测试失败(Testing Failure )

Forc supports testing failing cases for test functions declared with #[test(should_revert)]. For example:

Forc 支持用#[test(should_revert)] 声明的测试函数测试失败的案例。例如:

#[test(should_revert)]
fn test_meaning_of_life() {
    assert(6 * 6 == 42);
}

Tests with #[test(should_revert)] considered to be passing if they are reverting.

调用合约(Calling Contracts)

Unit tests can call contract functions an example for such calls can be seen below.

单元测试可以调用合约函数,下面可以看到此类调用的示例。

contract;

abi MyContract {
    fn test_function() -> bool;
}

impl MyContract for Contract {
    fn test_function() -> bool {
        true
    }
}

To test the test_function(), a unit test like the following can be written.

要测试 test_function(),可以编写如下单元测试。

#[test]
fn test_success() {
    let caller = abi(MyContract, CONTRACT_ID);
    let result = caller.test_function {}();
    assert(result == true)
}

It is also possible to test failure with contract calls as well.

也可以通过合约调用来测试失败。

#[test(should_revert)]
fn test_fail() {
    let caller = abi(MyContract, CONTRACT_ID);
    let result = caller.test_function {}();
    assert(result == false)
}

Note: When running forc test, your contract will be built twice: first without unit tests in order to determine the contract's ID, then a second time with unit tests with the CONTRACT_ID provided to their namespace. This CONTRACT_ID can be used with the abi cast to enable contract calls within unit tests.

注意: 当运行 forc test 时,您的合约将构建两次:第一次 不包括 without 单元测试来确定合约的 ID,然后第二次 包括with 单元测试来提供给其命名空间的 CONTRACT_ID。此 CONTRACT_ID 可与 abi cast 一起使用,以在单元测试中启用合约调用。

如果这些合约被添加为合约一来,即在 [contract-dependencies](https://fuellabs.github.io/sway/v0.38.0/book/forc/manifest_reference .html#the-contract-dependencies-section) 清单文件的部分。此类调用的示例如下所示:

contract;

abi CallerContract {
    fn test_false() -> bool;
}

impl CallerContract for Contract {
    fn test_false() -> bool {
        false
    }
}

abi CalleeContract {
    fn test_true() -> bool;
}

#[test]
fn test_multi_contract_calls() {
    let caller = abi(CallerContract, CONTRACT_ID);
    let callee = abi(CalleeContract, callee::CONTRACT_ID);

    let should_be_false = caller.test_false();
    let should_be_true = callee.test_true();
    assert(!should_be_false);
    assert(should_be_true);
}

Example Forc.toml for contract above:

上面合约的示例Forc.toml:

[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "caller"

[dependencies]
std = { path = "../../../sway-lib-std/" }

[contract-dependencies]
callee = { path = "../callee" }

并行或串行运行测试 (Running Tests in Parallel or Serially)

By default, all unit tests in your project are run in parallel. Note that this does not lead to any data races in storage because each unit test has its own storage space that is not shared by any other unit test.

默认情况下,项目中的所有单元测试都是并行运行的。请注意,这不会导致存储中的任何数据竞争,因为每个单元测试都有自己的存储空间,不与任何其他单元测试共享。

By default, forc test will use all the available threads in your system. To request that a specific number of threads be used, the flag --test-threads <val> can be provided to forc test.

默认情况下,forc test 将使用您系统中的所有可用线程。要请求使用特定数量的线程,可以将标志 --test-threads <val> 提供给 forc test。

forc test --test-threads 1

Visit the command reference to find the options available for forc test.

访问 命令,就可以参考/查找可用于 forc test 的选项。

Unit tests can call methods of external contracts if those contracts are added as contract dependencies, i.e. in the the section of the manifest file. An example of such calls is shown below:

script
testing failure
脚本
测试失败
forc test
forc test
contract-dependencies