> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tenderly.co/llms.txt
> Use this file to discover all available pages before exploring further.

# 本地开发和测试

> 使用 @tenderly/actions-test 加速对 Web3 Action 代码的迭代，并为您的 action 函数编写自动化测试。

为了便于本地开发您的 [Web3 Action](/monitoring/web3-actions/introduction) 代码，您可以使用 **@tenderly/actions-test** 包（包含在 tenderly-actions 中）。它暴露了一个 Tenderly 运行时，因此您可以在本地环境中执行并可能测试您的 Web3 Action。

安装 [**@tenderly/actions-test**](https://github.com/Tenderly/tenderly-actions/tree/main/packages/actions-test) 包以在本地测试您的 Web3 Action 函数。

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
yarn add --dev @tenderly/actions-test
```

<Note>
  建议将 web3-actions 的测试和运行脚本保留在 *actions root*
  目录之外，以避免增加 Web3 Action 项目的体积。
</Note>

您可以通过两种方式使用 **@tenderly/actions-test** 支持您的开发流程：

* 在您的自动化测试中使用它（**推荐**）。
* 用 JavaScript/TypeScript 创建运行脚本。

### 在您的测试中使用 @tenderly/actions-test

要在您的测试中使用 **@tenderly/actions-test**，无需额外步骤。导入 `TestRuntime` 以及测试 Web3 Action 所需的事件，并断言结果。

以下示例展示了如何使用该包调用 Web3 Action 并传入 Action 函数期望的事件对象。有关可传递事件类型的详细信息，请参阅 [函数、事件和触发器](/monitoring/web3-actions/references/functions-events-triggers)。

### 一个可运行的示例

查看多签钱包示例以及它如何使用 **@tenderly/actions-test** 在本地运行 action 函数。

<Card title="在本地运行 action 函数" href="" />

### 示例：在脚本中运行 Action 函数

以下示例展示了位于 **src/web3-actions-local/run.ts** 的文件，它运行三个 actions 并传入合适的事件。请注意，Web3 Actions 源代码文件（`src/actions/awesomeActions.ts`）位于包含该脚本目录的同级目录中：

#### 步骤 1：编写运行脚本

```tsx title="example.tsx" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
// File: src/web3-actions-local/run.ts
// web3-actions sources location: src/actions/awesomeActions.ts


/*
 * Running Web3 Actions code locally.
 * TestRuntime is a helper class that allows you to run the functions,
 * and set storage and secrets before running the function
 **/
const main = async () => {
  const testRuntime = new TestRuntime();

  testRuntime.context.secrets.put('multisig.DISCORD_URL', process.env.DISCORD_URL || '');

  await testRuntime.execute(everyDay, new TestPeriodicEvent());

  await testRuntime.execute(whenDappPosts, new TestWebhookEvent({ foo: 'bar' }));

  const te = new TestTransactionEvent();
  te.to = '0x1a22...';
  te.from = '0xc023...';

  await testRuntime.execute(onTxEvent, new TransactionEvent());
};

(async () => await main())();
```

#### 步骤 2：在所在项目的 package.json 中扩展 scripts。

您可以将此脚本添加到您的 **package.json** 文件中。

```diff title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
    ...
  "scripts": {
    ...
+   "local-w3a-run": "ts-node src/web3-actions-local/run.ts"
    ...
  }
  ...
}
```

#### 步骤 3：运行脚本

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
yarn local-w3a-run
```
