首页 > 解决方案 > 使用 Rust,您如何在发布到 crate.io 之前执行平台测试?

问题描述

我正在研究一个framework我正在测试我的Mac. 最终,我想发布到crate.io. 我希望它不会因为糟糕的平台测试而崩溃。有没有一种方法可以在所有或至少大多数当前部署平台上进行测试,而无需直接访问这些平台?例如,我无权访问Windows盒子。

标签: rustintegration-testing

解决方案


如果您将代码托管在 github 上,您可以设置 github 操作以在多个平台上构建和测试您的代码。

我有两组在我的代码上运行的操作。

  • 一个运行测试,并在 Linux 上进行剪辑和检查rust fmt,仅用于正常的推送和拉取请求
  • 另一个在设置发布分支并创建发布时运行,运行测试并构建和上传 Windows、Linux 和 macOS 的发布二进制文件。

您可以在这里看到完整的文件。

但是结合这些和简化意味着要对每个推送和拉取请求进行测试,您将创建一个.github/workflows/testing.yml像这样的文件(未经测试):

name: Run Tests

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build_matrix:
    name: Run tests for ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        name: [linux, windows, macos]
        include:
          - name: linux
            os: ubuntu-latest
          - name: windows
            os: windows-latest
          - name: macos
            os: macos-latest
    steps:
    - uses: actions/checkout@v1

    - uses: actions-rs/toolchain@v1
      with:
        profile: minimal
        toolchain: nightly
        override: true

    - name: Test
      run: cargo test

推荐阅读