RubyonRails–GUTUB动作上的最小值:并行测试

RubyonRails–GUTUB动作上的最小值:并行测试

时间:2021-4-19 作者:admin

如何运行RubyonRails测试?如果测试速度慢,该怎么办?如何管理复杂的工作流?您可以使用Gizub操作生成矩阵来在作业之间划分最小文件,并以更快的速度运行测试套件。

RubyonRails--GUTUB动作上的最小值:并行测试

如果您的最小测试耗时数十分钟,并且希望为您的Ruby工程团队节省一些时间,那么您可以在您的CI服务器上使用测试并行化。

要尽可能快地运行测试,需要将它们拆分为相等的桶(并行作业)。但是怎么做呢?有些测试文件可以非常快地执行,另一些最小的文件运行系统测试(E2E测试)可能需要几分钟的时间。

还需要为每个并行作业准备测试环境。我的意思是,你需要克隆一个存储库,安装红宝石或者从缓存中加载它们,也许你需要加载一些码头容器等等。这可能需要在每个并行作业上花费不同的时间。随机网络错误像网络延迟一样发生负载缓存宝石或者,与其他人相比,Gizub的行动有时会开始得很晚。在网络环境中,这是一个不可避免的问题,可能导致您的测试在每个并行作业上运行不同的时间。这在下面的图表中是可见的,它会导致CI构建的速度变慢。

RubyonRails--GUTUB动作上的最小值:并行测试

在一个完美的场景中,您想要解决所有这些问题,无论在并行作业中仍然能够将最小的工作分开,都要确保每个并行作业的测试在类似的时间完成。这不保证任何瓶颈。完美的测试分割在下面的图表上。

RubyonRails--GUTUB动作上的最小值:并行测试

具有队列模式的动态拆分测试

你可以用背包Pro队列模式在并行作业之间以动态方式拆分测试。这样,每个作业都会从队列中消耗测试,直到队列为空为止。简单地说,这允许您有效地利用CI服务器资源,并在最佳时间运行测试。

我描述队列模式如何将Ruby和JavaScript测试与动态测试套件拆分并行。你可以从那篇文章中学到。

GitHub操作构建矩阵以运行并行测试

GitHub操作有一个构造矩阵特征这允许同时运行多个作业。您可以使用它在并行作业之间运行最小的测试。

下面是一个针对Rails项目和Minitest的完整Gizub操作yml配置。测试与knapsack_proRuby创业板和队列模式。YAML
1

name: Main

23

on: [push]

45

jobs:

6

  test:

7

    runs-on: ubuntu-latest

89

    # If you need DB like PostgreSQL, Redis then define service below.

10

    # https://github.com/actions/example-services/tree/master/.github/workflows

11

    services:

12

      postgres:

13

        image: postgres:10.8

14

        env:

15

          POSTGRES_USER: postgres

16

          POSTGRES_PASSWORD: ""

17

          POSTGRES_DB: postgres

18

        ports:

19

          - 5432:5432

20

        # needed because the postgres container does not provide a healthcheck

21

        # tmpfs makes DB faster by using RAM

22

        options: >-

23

          --mount type=tmpfs,destination=/var/lib/postgresql/data

24

          --health-cmd pg_isready

25

          --health-interval 10s

26

          --health-timeout 5s

27

          --health-retries 5

28

      redis:

29

        image: redis

30

        ports:

31

          - 6379:6379

32

        options: --entrypoint redis-server

3334

    # https://help.github.com/en/articles/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix

35

    strategy:

36

      fail-fast: false

37

      matrix:

38

        # Set N number of parallel jobs you want to run tests on.

39

        # Use higher number if you have slow tests to split them on more parallel jobs.

40

        # Remember to update ci_node_index below to 0..N-1

41

        ci_node_total: [8]

42

        # set N-1 indexes for parallel jobs

43

        # When you run 2 parallel jobs then first job will have index 0, the second job will have index 1 etc

44

        ci_node_index: [0, 1, 2, 3, 4, 5, 6, 7]

4546

    env:

47

      RAILS_ENV: test

48

      PGHOST: localhost

49

      PGUSER: postgres

50

      # Rails verifies Time Zone in DB is the same as time zone of the Rails app

51

      TZ: "Europe/Warsaw"

5253

    steps:

54

      - uses: actions/checkout@v2

5556

      - name: Set up Ruby

57

        uses: ruby/setup-ruby@v1

58

        with:

59

          # Not needed with a .ruby-version file

60

          ruby-version: 2.7

61

          # runs 'bundle install' and caches installed gems automatically

62

          bundler-cache: true

6364

      - name: Create DB

65

        run: |

66

          bin/rails db:prepare

67

      - name: Run tests

68

        env:

69

          KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST: ${{ secrets.KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST }}

70

          KNAPSACK_PRO_CI_NODE_TOTAL: ${{ matrix.ci_node_total }}

71

          KNAPSACK_PRO_CI_NODE_INDEX: ${{ matrix.ci_node_index }}

72

          KNAPSACK_PRO_FIXED_QUEUE_SPLIT: true

73

          KNAPSACK_PRO_LOG_LEVEL: info

74

        run: |

75

          bundle exec rake knapsack_pro:queue:minitest

摘要

正如您所看到的,最慢的测试套件不需要成为您的问题。QA、测试人员或自动化工程师可以从提高CI构建速度和允许他们的软件开发团队更快地交付产品中获益。您可以在背包Pro.

结语

如果你需要小程序开发,可以点链接进行询问福州小程序开发

版权所有:https://www.eraycloud.com 转载请注明出处