跑单个用例通过,但是跑整个用例套失败

来源:6-13 视图的测试用例test_views.py

ian116

2020-11-12

views的测试,每个用例单独跑能通过,跑测试套就出现bad request,请问老师是什么原因?

报错如下:
Testing started at 20:28 …
ssh://ubuntu@152.32.132.203:22/home/ubuntu/.virtualenvs/venv/bin/python -u /home/ubuntu/.pycharm_helpers/pycharm/django_test_manage.py test zanhu.news.test.test_views /home/ubuntu/website/zanhu
Creating test database for alias ‘default’…
System check identified no issues (0 silenced).

WARNING 2020-11-12 20:29:04,541 log 1114 140198981699392 Bad Request: /news/get-thread/
Failure
Traceback (most recent call last):
File “/home/ubuntu/website/zanhu/zanhu/news/test/test_views.py”, line 72, in test_get_thread
assert response.status_code == 200
AssertionError

WARNING 2020-11-12 20:29:04,541 log 1114 140198981699392 Bad Request: /news/get-thread/
Failure
Traceback (most recent call last):
File “/home/ubuntu/website/zanhu/zanhu/news/test/test_views.py”, line 72, in test_get_thread
assert response.status_code == 200
AssertionError

WARNING 2020-11-12 20:29:04,663 log 1114 140198981699392 Bad Request: /news/like/
Failure
Traceback (most recent call last):
File “/home/ubuntu/website/zanhu/zanhu/news/test/test_views.py”, line 62, in test_like_news
assert response.status_code == 200
AssertionError

WARNING 2020-11-12 20:29:05,127 log 1114 140198981699392 Bad Request: /news/post-comment/
Failure
Traceback (most recent call last):
File “/home/ubuntu/website/zanhu/zanhu/news/test/test_views.py”, line 87, in test_post_comments
assert response.status_code == 200
AssertionError

Destroying test database for alias ‘default’…
Process finished with exit code 1

图片描述
图片描述
图片描述

写回答

2回答

归来也无风雨也无晴

2021-08-18

def test_delete_view(self):
    init_total = News.objects.count()
    # response = self.other_client.post(reverse('news:delete_news', kwargs={'pk': self.second_news.pk}))
    response = self.client.post(reverse('news:delete_news', kwargs={'pk': self.second_news.pk}))
    assert response.status_code == 302
    self.assertEqual(News.objects.count(), init_total - 1)
    
    问题:
    self.client是用first_user登录的,用这个client删除第一条动态肯定是不行的,用self.other_client就成功了
1
1
Jack
谢谢!
2021-08-19
共1条回复

Jack

2020-11-13

您好,报Traceback 错误的话,不是测试结果失败,而是测试用例本身写的有问题报错了,能否截图一下你测试用例的完整代码呢?当然还有一种可能,你测试用例写得和视频里一样,但是被测试的功能写错了

0
3
ian116
回复
Jack
单独运行任意函数都没有报错。算了 不纠结了 往下学习了
2020-11-17
共3条回复

Django高级实战 开发企业级问答网站

融合Django高级用法/算法/设计模式/TestCase测试/云计算打造项目

900 学习 · 756 问题

查看课程