github集成自动化测试circleci

作者: dreamfly 分类: ci/cd 发布时间: 2021-09-29 21:31

自动化测试

持续集成和持续部署是现在软件开发中必不可少的过程,任何软件都需要经过不断迭代,而每次迭代都需要进行大量的功能和单元测试,因此开发人员开发出了自动化测试框架,这样每次进行功能迭代的时候都会进行自动测试,如果没有问题就可以实现自动部署。

CircleCI

CircleCI是面向全球最佳工程团队的持续集成平台,它支持docker部署,它有自己的镜像源,支持docker-compose管理,它支持公有云部署,也支持私有云部署,你可以自己搭建集成测试环境,也可以使用它提供的服务器进行集成测试。

使用步骤

首先你需要注册一个账号,当然了,你也可以直接使用github账号进行授权登录,这样的化,你就相当于将一部分权限授权给circleci,这样你的github的仓库就可以使用它进行集成测试。

登录之后,你可以进入它的管理后台,然后你会看见你的github上创建的公共仓库,你有两种方式来进行配置你的项目。

  • 通过它提供的向导来生成配置文件
  • 自己去github的项目中创建.circleci/config.yml文件

一开始推荐你采用第一种方式,因为它会给你模板进行修改,你可以根据你的项目语言,快速选择对应的配置文件模板,然后它会自动写入到你的github项目中,然后并进行第一次集成测试。

配置说明

下面是一个简单的node的配置文件

# Use the latest 2.1 version of CircleCI pipeline process engine.
# See: https://circleci.com/docs/2.0/configuration-reference
version: 2.1

orbs:
  # The Node.js orb contains a set of prepackaged CircleCI configuration you can utilize
  # Orbs reduce the amount of configuration required for common tasks.
  # See the orb documentation here: https://circleci.com/developer/orbs/orb/circleci/node
  node: circleci/node@4.7

jobs:
  # Below is the definition of your job to build and test your app, you can rename and customize it as you want.
  build-and-test:
    # These next lines define a Docker executor: https://circleci.com/docs/2.0/executor-types/
    # You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub.
    # A list of available CircleCI Docker Convenience Images are available here: https://circleci.com/developer/images/image/cimg/node
    docker:
      - image: cimg/node:16.10
    # Then run your tests!
    # CircleCI will report the results back to your VCS provider.
    steps:
      # Checkout the code as the first step.
      - checkout
      # Next, the node orb's install-packages step will install the dependencies from a package.json.
      # The orb install-packages step will also automatically cache them for faster future runs.
      - node/install-packages:
          # If you are using yarn, change the line below from "npm" to "yarn"
          pkg-manager: npm
      - run:
          name: Run tests
          command: npm test

workflows:
  # Below is the definition of your workflow.
  # Inside the workflow, you provide the jobs you want to run, e.g this workflow runs the build-and-test job above.
  # CircleCI will run this workflow on every commit.
  # For more details on extending your workflow, see the configuration docs: https://circleci.com/docs/2.0/configuration-reference/#workflows
  sample:
    jobs:
      - build-and-test
      # For running simple node tests, you could optionally use the node/test job from the orb to replicate and replace the job above in fewer lines.
      # - node/test

我们简单来说明下这个文件,至于具体的每个参数,可以到官方进行手册查询。

version指的是版本号,不同版本的参数可能有不同的定义,这个需要注意
orbs 这是一个circleci提供的官方仓库,在这里你可以选择不同的版本环境,比如不同版本的jdk,不同版本的php,不同版本的node,不同版本的.net等等。
jobs这个是配置的关键,我们要执行的测试命令,环境配置都是在这里进行配置。
build-and-test这是测试配置文件名,这里我们可以进行自定义
docker这个使用来配置我们的测试环境的,一般这里可以选择node环境,或者php环境,或者golang环境,这里的版本可以在官网找到。
steps后买的是具体的操作命令,比如签出代码,环境初始化,安装依赖,进行测试。
checkout表示在测试环境中拉去github上的代码
node/install-packages这里主要指的是配置一些依赖,比如npm install,composer install等。
run指的是要执行的一些命令,每个命令都有一些参数,下面的是我从官网截取的参数。

command Y   String  Command to run via the shell
name    N   String  Title of the step to be shown in the CircleCI UI (default: full command)
shell   N   String  Shell to use for execution command (default: See Default Shell Options)
environment N   Map Additional environmental variables, locally scoped to command
background  N   Boolean Whether or not this step should run in the background (default: false)
working_directory   N   String  In which directory to run this step. Will be interpreted relative to the working_directory of the job). (default: .)
no_output_timeout   N   String  Elapsed time the command can run without output. The string is a decimal with unit suffix, such as “20m”, “1.25h”, “5s”. The default is 10 minutes and the maximum is governed by the maximum time a job is allowed to run.
when    N   String  Specify when to enable or disable the step. Takes the following values: always, on_success, on_fail (default: on_success)

后面的workflows表示工作流,可以看到一个workflows里面可以创建多个jobs.

数据库测试

对于普通的测试没有什么需要特殊说明的,需要特殊说明的是数据库测试,circleci是支持多种数据库测试的,这里我以mysql为例。

ersion: 2.0
jobs:
  build:
    working_directory: /your/workdir
    docker:
      - image: your/image_for_primary_container
        auth:
          username: mydockerhub-user
          password: $DOCKERHUB_PASSWORD  # context / project UI env-var reference
      - image: mysql:5.6.50

        environment:
          MYSQL_DATABASE: test_db
          MYSQL_USER: user
          MYSQL_PASSWORD: passw0rd
    steps:
      - checkout
      - run:
          name: install dockerize
          command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && sudo tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
          environment:
            DOCKERIZE_VERSION: v0.3.0
      - run:
          name: Wait for db
          command: dockerize -wait tcp://localhost:3306 -timeout 1m

mysql容器安装的时候,是不能进行之后的测试的,我们需要等待mysql连接成功才可以进行测试,因此

dockerize -wait tcp://localhost:3306 -timeout 1m

就是在等待mysql容器创建成功,调用这个之后,我们再进行后面的测试。

install dockerize

这个命令非常关键,否则你会发现你的mysql虽然从镜像源拉去安装成功了,但是就是无法连接成功,而加上这个容器初始化,就可以连接成功了。

总结

对于circleci的测试,其实还有很多细节需要注意,而这些细节就是平时你在使用的时候多注意文档才能避免犯错误,另外需要注意的是,一定要注意版本,不同的版本,可能命令会有不同,就是不同的mysql版本,可能对于数据库的初始化,密码的初始化都是有不同的关联的,在遇到问题的时候,第一个是先看看官方文档是否有说明,要记住,官网的搜索功能其实很强大的。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

评论已关闭!