我正在使用django进行单元测试
在items / tests.py
中class NewBookSaleTest(SetUpLogInMixin):
def test_client_post_books(self):
send_post_data_post = self.client.post(
'/booksale/',
data = {
'title':'Book_A',
}
)
new_post = ItemPost.objects.first()
self.assertEqual(new_post.title, 'Book_A')
在views / booksale.py
中class BookSale(LoginRequiredMixin, View):
login_url = '/login/'
def get(self, request):
[...]
def post(self, request):
title = request.POST.get('title')
saler = request.user
created_bookpost = ItemPost.objects.create(
user=saler,
title=title,
)
# redis + celery task queue
auto_indexing = UpdateIndexTask()
auto_indexing.delay()
return redirect(
[...]
)
当我运行单元测试时,引发redis连接错误
redis.exceptions.ConnectionError
我知道当我运行redis-server和celery时会出错,会解决
但是当我在Travis CI
中运行单元测试时,我无法在Travis CI中运行redis-server和celery
所以,我发现了这个link
我尝试在.travis.yml中插入此代码
language:
python
python:
- 3.5.1
addons:
postgresql:"9.5.1"
install:
- pip install -r requirement/development.txt
service:
- redis-server
# # command to run tests
script:
- pep8
- python wef/manage.py makemigrations users items
- python wef/manage.py migrate
- python wef/manage.py collectstatic --settings=wef.settings.development --noinput
- python wef/manage.py test users items --settings=wef.settings.development
但它显示相同的错误
所以我找到了下一个link
before_script:
- sudo redis-server /etc/redis/redis.conf --port 6379 --requirepass 'secret'
但......它显示相同的错误......
如何在travis ci中运行redis-server?
答案 0 :(得分:1)
如果您现在还没有解决问题,这里有一个解决方案。
删除服务热线。
Redis是测试环境提供的默认组件,因此
service:
- redis-server
将被翻译为:
service redis start
在此问题中,我们要自定义redis以添加密码auth。因此,我们不需要travis ci即可启动Redis服务。只需使用before_script
。
毕竟,您的.travis.yml应该是这样的:
language:
python
python:
- 3.5.1
addons:
postgresql:"9.5.1"
install:
- pip install -r requirement/development.txt
before_script:
- sudo redis-server /etc/redis/redis.conf --port 6379 --requirepass 'secret'
# # command to run tests
script:
- pep8
- python wef/manage.py makemigrations users items
- python wef/manage.py migrate
- python wef/manage.py collectstatic --settings=wef.settings.development --noinput
- python wef/manage.py test users items --settings=wef.settings.development