# CosmWasm合约测试 (CosmWasm Contract Testing)

### 单元测试 (Unit Testing)

To make tests, specify:

要测试，要明确：

```
#[cfg(test)]

mod tests { ...

}
```

To run tests: `cargo test`

运行测试：`cargo test`

Cosmwasm Reference: <https://docs.cosmwasm.com/tutorials/simple-option/testing/>

Cosmwasm 参考：<https://docs.cosmwasm.com/tutorials/simple-option/testing/>

Example of a basic contract and tests: <https://github.com/InterWasm/cw-contracts/blob/main/contracts/simple-option/src/contract.rs>

基本合约和测试示例：\[<https://github.com/InterWasm/cw-contracts/blob/main/contracts/simple-option/src/contract.rs]\\(https://github.com/InterWasm/> cw-contracts/blob/main/contracts/simple-option/src/contract.rs)

### 集成测试 (Integration Testing)

The `cw-multi-test` (<https://docs.rs/cw-multi-test/latest/cw_multi_test/>) package allows for integration tests.

`cw-multi-test` (<https://docs.rs/cw-multi-test/latest/cw_multi_test/>)包允许集成测试。

We can do the following steps:\
我们可以执行以下步骤：

1. Initiate the testing setup, which may look like the following, where we use `instantiate_contract()`and then can use `execute_contract()` accordingly:\
   启动测试设置，可能如下所示，我们使用 `instantiate_contract()`，然后可以相应地使用 `execute_contract()`：

```
fn setup_test(
    app: &mut App<
        BankKeeper,
        MockApi,
        MockStorage,
        SeiModule,
        WasmKeeper<SeiMsg, SeiQueryWrapper>,
        FailingStaking,
        FailingDistribution,
    >,
) -> (Addr, Addr) {
    let example_contract_code = app.store_code(Box::new(
        ContractWrapper::new(
            example_crate::contract::execute,
            example_crate::contract::instantiate,
            example_crate::contract::query,
        )
        .with_reply(example_crate::contract::reply),
    ));

    let example_contract_addr = app
        .instantiate_contract(
            example_contract_code,
            Addr::unchecked(ADMIN),
            &example_crate::msg::InstantiateMsg {
                example_param: Uint64::new(100),
            },
            &[],
            "example",
            Some(ADMIN.to_string()),
        )
        .unwrap();

    app.execute_contract(
        Addr::unchecked(ADMIN),
        example_contract_addr.clone(),
        &ExampleExecuteMsg::ExampleCall {
            asset_denom: "usei".to_string(),
        },
        &[],
    )
    .unwrap();

    example_contract_addr
}
```

Cosmwasm reference: <https://docs.cosmwasm.com/docs/1.0/smart-contracts/testing/>

Cosmwasm 参考：<https://docs.cosmwasm.com/docs/1.0/smart-contracts/testing/>

### 使用 Sei 链进行端到端测试 (End-to-end testing with Sei chain)

In order to do end-to-end testing, follow the previous steps to 1) launch the chain either locally or on devnet: <https://docs.seinetwork.io/smart-contracts-and-local-development/set-up-a-local-network>, and 2) deploy your contract: <https://docs.seinetwork.io/smart-contracts-and-local-development/deploy-a-generic-contract>. For an example contract that can be used for local testing see the following: <https://github.com/sei-protocol/sei-cosmwasm>

为了进行端到端测试，请按照前面的步骤 1) 在本地或 devnet 上启动链：[https://docs.seinetwork.io/smart-contracts-and-local-development/set- up-a-local-network](https://docs.seinetwork.io/smart-contracts-and-local-development/set-up-a-local-network)，以及 2) 部署你的合约：\[https: //docs.seinetwork.io/smart-contracts-and-local-development/deploy-a-generic-contract]\(<https://docs.seinetwork.io/smart-contracts-and-local-development/deploy-a> -通用合同)。有关可用于本地测试的示例合约，请参见以下内容：<https://github.com/sei-protocol/sei-cosmwasm>

For debugging, such as if an order is unsuccessful, one can use a combination of tools on the contract side. Firstly, the Rust compiler can catch issues at compile time. Secondly, for any `struct` that implements the `Debug` trait then `println!` can be used. Lastly, logs can be added via `deps.api.debug(err)` on the contract side.

对于调试，如果订单不成功，可以使用合约端的工具组合。首先，Rust 编译器可以在编译时发现问题。其次，对于任何实现了 `Debug` 特性的 `struct`，都可以使用 `println!`。最后，可以通过合约端的 `deps.api.debug(err)` 添加日志。
