对 Github 操作运行端到端测试

GN.*_*GN. 4 github-actions

如何对 Github 操作运行端到端测试?

我试图弄清楚如何启动服务器,以便端到端测试套件可以在其上运行。

在我们的示例中,我们有一个 Rails 应用程序,其中包含一些 Cucumber 和 Cypress 测试。

小智 5

您可以尝试使用 Python 运行服务器http.server或启动 Rails 服务器。

首先,在应用程序的根目录中创建一个路径.github/workflows,然后test.yaml在 /workflows 中创建一个文件。

添加 Ruby 的基本配置,大致如下:

# .github/workflows/test.yml

name: Test
on: push  # Trigger on push

jobs:
  test:
    name: Run tests
    runs-on: ubuntu-16.04 # Specific version for Cypress

    steps:
    - name: Checkout repository
      uses: actions/checkout@v2
    - name: Set up Ruby
      uses: ruby/setup-ruby@v1
      with: 
        ruby-version: '2.6' # Specify your version
    - name: Cache gems # You can cache your gems to make the workflow run faster
      uses: actions/cache@v2
      with:
        path: vendor/bundle
        key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
        restore-keys: |
          ${{ runner.os }}-gems-
    - name: Install gems
      run: |
        bundle config path vendor/bundle
        bundle install --jobs 4 --retry 3

    - name: Start server in the background
      run: |
        bundle exec rails server &
        sleep 5 && 
        curl http://localhost:3000 -I

    # Add step for running cucumber tests here
    
    - name: Run Cypress
      uses: cypress-io/github-action@v1
      with:
        browser: chrome
        headless: true
    - name: Upload screenshots as artifacts
      uses: actions/upload-artifact@v1
      if: failure()
      with:
        name: cypress-screenshots
        path: cypress/screenshots
    - name: Upload videos as artifacts
      uses: actions/upload-artifact@v1
      if: always()
      with:
        name: cypress-videos
        path: cypress/videos

Run Code Online (Sandbox Code Playgroud)

当您提交文件并推送时,请转到 github 存储库的“操作”选项卡以检查它是否有效或是否需要调试。

另外,我建议您查看这篇文章https://boringrails.com/articles/building-a-rails-ci-pipeline-with-github-actions/