获得问题的“评论”/添加评论#15
Conversation
| self.assertEqual('', data) | ||
|
|
||
| def test_get_comments_with_id(self): | ||
| total, data = Question(id=23497514).get_comments() # id=37050422的评论较多,250多条 |
There was a problem hiding this comment.
- 应该选评论适量的问题,而不是“较多”的。
- 其实测试不应该依赖远端数据的。不然我本地离线开发还跑不了测试了?或者我的测试居然会根据远端网站实际的运行情况成功或者失败?最好把这些实际的网络请求都
mock掉。当然你这么写大概是因为这个仓库的作者之前就这么写了,好像也不能怪你。。。 - 接口返回最好只有一个对象。你可以选择包装成一个
namedtuple或者一个实际的Object。例如,这个接口其实可以返回一个list[Comment]的。
|
|
||
| def test_get_comments_with_url(self): | ||
| total, data = Question(url='https://www.zhihu.com/question/23497514').get_comments() | ||
| self.assertEqual(type(total), type(0)) |
There was a problem hiding this comment.
用 isinstance 不好么?为嘛要用 type(0) 这种东西?其他地方类似。
| self.assertEqual(type(data), type([])) | ||
|
|
||
| def test_make_comments_with_id(self): | ||
| data = Question(id=22519728).make_comments(u"好") |
There was a problem hiding this comment.
- 添加评论这个接口,叫
add_comment比较合适。不是make,也不是comments。 - 如上面所说,我跑一次测试就 真的 在远端网站添加了一条评论?要被打的小伙子。
| data = Question(id=22519728).make_comments(u"好") | ||
| self.assertIn('allow_reply', data) | ||
|
|
||
| def test_make_comments_with_url(self): |
There was a problem hiding this comment.
其实这个测试没有意义。因为 Question 不是你写的。
这个测试其实是需要写 Question 这个类的人保证通过 id 和 url 创建的 Question 具备相同的操作行为。
| totals = dataObj['paging']['totals'] | ||
| comments.append(dataObj['data']) | ||
| # 每一次请求返回10条,循环以获得全部评论 | ||
| while not dataObj['paging']['is_end']: |
| @need_login | ||
| def make_comments(self, comment, **kwargs): | ||
| """添加评论""" | ||
| comment = comment |
| def get_comments(question_id): | ||
| return URL.host + "/api/v4/questions/{id}/comments".format(id=question_id) | ||
|
|
||
| make_comments = get_comments |
|
@Fity 我在想一个问题,如果用mock的话,我如何保证我的接口是正确的,加入知乎有更新,我也发现不了,所以... |
|
@lzjun567 小伙子妳再想想?你这个是写在测试用例里了,那如果你不跑测试用例呢?你写测试用例的目的到底是为了验证自己代码写的没错还是验证和知乎的交互协议没问题? |
|
@Fity 两个都要验证,错了一个都不行。 现在这个测试肯定是不完善的。 |
|
@lzjun567 其实你的测试用例只需要(并且只应该)测试你自己的代码没有问题。协议的问题应该在另外的地方验证。这和你测试玩不完善没什么关系。 |
|
@Fity 多谢建言 我现在遇到的问题是,如果你现在发了一个PR,单元测试用Mock,那么我如何验证你的代码是否真的可以正确调用知乎的接口呢,比如你写了一个评论接口,能不能真的评论成功,如何判断,还是放任不管,等调用者来发现问题? 另外,你说的在另外的地方验证,那么这个另外的地方是指什么地方,以及它的时机是什么? |
No description provided.