【发布时间】:2021-06-26 20:53:51
【问题描述】:
我正在创建一个 Django 项目,并且我创建了一个继承自 django.test.LiveSeverTestCase 的测试用例(因此它使用 selenium):
from django.test import LiveServerTestCase
from selenium.webdriver.chrome.webdriver import WebDriver
class FrontEndTestCase(LiveServerTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.selenium = WebDriver()
cls.selenium.implicitly_wait(10)
# Get the URL
cls.selenium.get('http://127.0.0.1:8000/vota/')
# Add CSRFToken
cls.selenium.add_cookie({'name': 'csrftoken', 'value': '1cY4Yb3SljOqj9tUndW1YlIokNOD8tNc2MSU5iKNvsZW8co9WoOOCVGd5RFzxD8P'})
# Define page sections
cls.choose_comps = cls.selenium.find_element_by_id('choose-comps')
cls.poll = cls.selenium.find_element_by_id('poll-container')
cls.end_table = cls.selenium.find_element_by_id('table-container')
cls.navs = cls.selenium.find_elements_by_class_name('nav-link')
@classmethod
def tearDownClass(cls):
cls.selenium.quit()
super().tearDownClass()
这些测试与我的问题无关,所以为了长篇大论,我不会包括它们。您可能已经意识到,要使这个测试用例起作用,我必须使用python manage.py runserver 启动服务器。
这里的问题是我想在 GitHub Actions 中使用所有这些测试,到目前为止,我一直在使用,直到创建这个测试用例。由于我必须先启动服务器,因此我在工作中迈出了新的一步。但是这一步永远不会结束,因为服务器将始终监听进一步的请求。我还必须以某种方式安装 chromedriver,我不知道该怎么做。
这是我的 ci.yml 文件:
name: Testing
on: push
jobs:
test_vote:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:10.8
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: juradofms
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
python-version: 3.7
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run migrations
run: python manage.py migrate
env:
# Random key
SECRET_KEY: '!nj1v)#-y)e21t^u@-6tk+%+#vyzn30dp+)xof4q*y8y&%=h9l'
- name: Runserver
run: python manage.py runserver
env:
# Random key
SECRET_KEY: '!nj1v)#-y)e21t^u@-6tk+%+#vyzn30dp+)xof4q*y8y&%=h9l'
- name: Test
run: python manage.py test
env:
# Random key
SECRET_KEY: '!nj1v)#-y)e21t^u@-6tk+%+#vyzn30dp+)xof4q*y8y&%=h9l'
我对 runserver 问题的解决方案是以某种方式在另一个线程上运行此命令,我该怎么做?
【问题讨论】:
标签: django selenium github-actions