首页 > 解决方案 > 使用 Github 操作的自动 Django API 测试失败

问题描述

我想在 Github 的每次推送期间测试自动测试。所以我写了一个基本的 API 和测试用例。在我使用 ubuntu 的本地机器上,一切正常。我将代码上传到 GitHub 存储库,并编写了 GitHub 工作流程,如下所示:

  name: testing
  
  on:
  
    push:
      branches: [ main ]
    pull_request:
      branches: [ main ]
  
  jobs:
    test-code:
      runs-on: ubuntu-latest
      steps:
        - name: Checkout repo
          uses: actions/checkout@v2
        - name: Set up python
          uses: actions/setup-python@v2
          with:
            python-version: 3.8.8
        - name: Caching
          uses: actions/cache@v2
          with:
            path: $/{/{ env.pythonLocation /}/}
            key: $/{/{ env.pythonLocation /}/}-$/{/{ hashFiles('setup.py') /}/}-$/{/{ hashFiles('requirements.txt') /}/}
  
        - name: Install dependencies
          run: python -m pip install -r requirements.txt
  
        - name: Run django integratin test
          run: python manage.py test
  

我的测试代码就像下面这样:

        PREDICT_API_URL = 'http://127.0.0.1:8000/predict/'
        
        class PredictApi(TestCase):
            '''
            Using postman these can be tested as well.
            '''
        
            @tag('important')
            def test_predict_negative(self):
                # python manage.py test predict.tests.PredictApi.test_predict_negative
        
                data = {
                    "cough": [0],
                    "fever": [0],
                    "sore_throat": [0],
                    "shortness_of_breath": [0],
                    "head_ache": [0],
                    "age_60_and_above": [0],
                    "gender": [0],
                    "test_indication": [0]
                }
        
                response = requests.post(PREDICT_API_URL, json=data)
                results = response.json()
        
                assert results['corona'] == 0      

但是当我推送到 GitHub 时,我遇到了连接错误。

在此处输入图像描述

我正在学习 Github 操作。如果我让它运行会很好。这个的 Github 链接是:https ://github.com/bikashckarmokar/covid_prediction

标签: django-rest-frameworkautomated-testshttp-postgithub-actions

解决方案


推荐阅读