ruby 如何在github中使用parallel_tests

a7qyws3x  于 11个月前  发布在  Ruby
关注(0)|答案(3)|浏览(102)

我试图在github action中使用parallel_tests来运行我的测试套件,但我无法找到合适的解决方案。官方文档中有一个,但它是针对gitlab的:
https://github.com/grosser/parallel_tests/wiki/Distributed-Parallel-Tests-on-CI-systems
任何帮助将不胜感激!

qyyhg6bp

qyyhg6bp1#

下面是一个可以放入.github/workflows/tests.yml的示例工作流:

name: Rails Tests

on: push

env:
  PGHOST: localhost
  PGUSER: postgres
  RAILS_ENV: test

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        # Set N number of parallel jobs you want to run
        # Remember to update ci_node_index below to 0..N-1
        ci_node_total: [6]
        # set N-1 indexes for parallel jobs
        # When you run 2 parallel jobs then first job will have index 0, the second job will have index 1 etc
        ci_node_index: [0, 1, 2, 3, 4, 5]

    services:
      postgres:
        image: postgres:11.5
        ports: ["5432:5432"]
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
      redis:
        image: redis:5
        ports: ["6379:6379"]

    steps:
    - uses: actions/checkout@v1

    - uses: ruby/setup-ruby@v1
      with:
        bundler-cache: true

    - name: Set node version (from .tool-versions)
      run: echo "NODE_VERSION=$(cat .tool-versions | grep nodejs | sed 's/^nodejs //')" >> $GITHUB_ENV

    - uses: actions/setup-node@v2
      with:
        node-version: ${{ env.NODE_VERSION }}

    - uses: bahmutov/npm-install@v1

    - name: Install PostgreSQL client
      run: |
        sudo apt-get -yqq install libpq-dev postgresql-client

    - name: Test Prep
      env:
        CI_NODE_INDEX: ${{ matrix.ci_node_index }}
      run: |
        bundle exec rake parallel:create["1"] parallel:load_schema["1"]

    - name: Run tests
      env:
        RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
        CI_NODE_TOTAL: ${{ matrix.ci_node_total }}
        CI_NODE_INDEX: ${{ matrix.ci_node_index }}
      run : |
        bundle exec parallel_test spec/ -n $CI_NODE_TOTAL --only-group $CI_NODE_INDEX --type rspec

字符串

58wvjzkj

58wvjzkj2#

这就是我用来解决它的方法。注意:还有一些其他的省略部分,比如ruby,postgresql,sqlite等的设置。

name: "Lint and Test"
jobs:
  test:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    services:
      redis:
        image: redis
        ports: ["6379:6379"]
      postgres:
        ports: ["5432:5432"]
    steps:
        //omitted setups
      - name: Setup Parallel Database
        env:
          RAILS_ENV: test
          PGHOST: localhost
          PGUSER: postgres
        run: |
          cp config/database.yml.example config/database.yml
          bundle exec rake parallel:create
          bundle exec rake parallel:rake[db:schema:load] || true
      - name: Build and test with rspec
        env:
          RAILS_ENV: test
          PGHOST: localhost
          PGUSER: postgres
          APP_REDIS_URL: redis://localhost:6379
          MINIMUM_COVERAGE: 80
        run: |
          bundle exec parallel_rspec --verbose spec

字符串

irtuqstp

irtuqstp3#

我写了一篇关于这个的博客(link)。当前接受的答案在一个runner中运行所有组,这并没有真正向外扩展。Benjamin Curtis的答案确实向外扩展,但只向每个runner发送一个组,对于我的用例来说,这并没有真正利用runner的资源。在我遵循的方法中,我也使用了矩阵策略,而是向每个跑步者发送3组。
以下是相关的片段:

run_unit_tests_chunk:
   # this is the job for the unit tests (non-integration tests)
   strategy:
     matrix:
       groups: [ "[1,2,3]", "[4,5,6]", "[7,8,9]", "[10,11,12]"]
   uses: ./.github/workflows/ci_unit_tests_chunk.yml
   secrets: inherit
   with:
     groups: ${{ matrix.groups }}
     group_count: 12 # the total number of test groups, must match the groups listed in the matrix.groups
     parallel_processes_count: 3 # the number of parallel processes to run tests in worker, must match the size of the
                                 # inner arrays in the matrix.groups

字符串
你可以在我写的博客文章(link)上读到更多。

相关问题