【问题标题】:Is possible run command on different terminal on Github action?是否可以在 Github 操作的不同终端上运行命令?
【发布时间】:2021-11-17 00:01:05
【问题描述】:

1.目前,我正在构建一个烧瓶项目,并且我还编写了一些单元测试代码。现在我想在 GitHub 操作上运行单元测试,但它卡在 ./run 阶段(./run 将打开http://127.0.0.1:5000/),并且没有运行 $pytest 命令。我知道 $pytest 不会执行的原因是因为 Github Action 正在运行端口http://127.0.0.1:5000/。在这种情况下,它无法执行./run 之后的任何命令。我想知道我可以在 GitHub 操作中的另一个终端上运行 $pytest 吗?这可能吗?

这是我的 github 操作的输出:

Run cd Flask-backend
  cd Flask-backend
  ./run
  pytest 
  shell: /usr/bin/bash -e {0}
  env:
    pythonLocation: /opt/hostedtoolcache/Python/3.8.10/x64
 * Serving Flask app 'app.py' (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 404-425-256

2.这是我的 yml 文件代码:

name: UnitTesting
on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install Python 3
        uses: actions/setup-python@v1
        with:
          python-version: 3.8
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirement.txt
          
      - name: Run tests with pytest
        run: |
           cd Flask-backend
           ./run
           pytest 

【问题讨论】:

    标签: flask github github-actions


    【解决方案1】:

    您可以使用nohup 命令在后台运行flask 服务器,而不是在不同的终端上运行。

    nohup python app.py
    

    在使用sleep 命令运行此命令后等待一段时间,然后运行您的测试。

    【讨论】:

    • 但是如果我想关闭端口怎么关闭呢?
    • kill -9 $(lsof -i :5000 | awk 'FNR > 1 {print $2}')
    • Sorry.. 我刚试过你说的,它仍然只运行服务器。 pytest没有执行成功
    • 只需尝试简单的 curl 即可查看服务器是否可访问,并检查在后台运行应用程序时是否有任何错误
    【解决方案2】:

    我尝试了几次,但都没有成功。但是有一个简单的解决方法可以在本地测试您的 api。我通过使用烧瓶包中的 test_client() 使用 pytest 脚本实现了它。此客户端模拟您的烧瓶应用程序。

    from api import app
    
    # init test_client
    app.config['TESTING'] = True
    client = app.test_client()
    
    # to test your app
    def test_api():
       r = client.get('/your_endpoint')
       assert r.status_code == 200
    

    请注意,现在每个请求都直接通过客户端及其所属的方法进行。 你可以在这里找到更多信息:https://flask.palletsprojects.com/en/2.0.x/testing/

    【讨论】:

      猜你喜欢
      • 2018-01-30
      • 2018-07-02
      • 2017-01-09
      • 2023-03-13
      • 2017-04-08
      • 2022-06-14
      • 2021-06-08
      • 2022-01-06
      • 1970-01-01
      相关资源
      最近更新 更多