Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@chenghengchao
Copy link
Contributor

No description provided.

Copy link

@Fity Fity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

小伙子还要加油,看好你哟~
@lzjun567 小伙子你也加油~

test/question.py Outdated
self.assertEqual('', data)

def test_get_comments_with_id(self):
total, data = Question(id=23497514).get_comments() # id=37050422的评论较多,250多条
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 应该选评论适量的问题,而不是“较多”的。
  2. 其实测试不应该依赖远端数据的。不然我本地离线开发还跑不了测试了?或者我的测试居然会根据远端网站实际的运行情况成功或者失败?最好把这些实际的网络请求都 mock 掉。当然你这么写大概是因为这个仓库的作者之前就这么写了,好像也不能怪你。。。
  3. 接口返回最好只有一个对象。你可以选择包装成一个 namedtuple 或者一个实际的 Object。例如,这个接口其实可以返回一个 list[Comment] 的。

test/question.py Outdated

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))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isinstance 不好么?为嘛要用 type(0) 这种东西?其他地方类似。

test/question.py Outdated
self.assertEqual(type(data), type([]))

def test_make_comments_with_id(self):
data = Question(id=22519728).make_comments(u"好")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 添加评论这个接口,叫 add_comment 比较合适。不是 make ,也不是 comments
  2. 如上面所说,我跑一次测试就 真的 在远端网站添加了一条评论?要被打的小伙子。

test/question.py Outdated
data = Question(id=22519728).make_comments(u"好")
self.assertIn('allow_reply', data)

def test_make_comments_with_url(self):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实这个测试没有意义。因为 Question 不是你写的。
这个测试其实是需要写 Question 这个类的人保证通过 idurl 创建的 Question 具备相同的操作行为。

totals = dataObj['paging']['totals']
comments.append(dataObj['data'])
# 每一次请求返回10条,循环以获得全部评论
while not dataObj['paging']['is_end']:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

写个 while True + break 不就能把上面那次操作去掉了么。。。

@need_login
def make_comments(self, comment, **kwargs):
"""添加评论"""
comment = comment
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没必要的赋值。。。

def get_comments(question_id):
return URL.host + "/api/v4/questions/{id}/comments".format(id=question_id)

make_comments = get_comments
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一行是要干嘛?没看懂

@lzjun567
Copy link
Owner

lzjun567 commented May 3, 2017

@Fity 我在想一个问题,如果用mock的话,我如何保证我的接口是正确的,加入知乎有更新,我也发现不了,所以...

@Fity
Copy link

Fity commented May 4, 2017

@lzjun567 小伙子妳再想想?你这个是写在测试用例里了,那如果你不跑测试用例呢?你写测试用例的目的到底是为了验证自己代码写的没错还是验证和知乎的交互协议没问题?

@lzjun567
Copy link
Owner

lzjun567 commented May 4, 2017

@Fity 两个都要验证,错了一个都不行。

现在这个测试肯定是不完善的。

@Fity
Copy link

Fity commented May 4, 2017

@lzjun567 其实你的测试用例只需要(并且只应该)测试你自己的代码没有问题。协议的问题应该在另外的地方验证。这和你测试玩不完善没什么关系。
你想想你跑测试的时机。你代码写完了,没什么原因的话你是不会去改代码的,然后也就不会去跑测试了。那这段时间内协议如果变了呢?你怎么知道这个变化?
小伙子还不错的,再想想。当上CTO,迎娶百富美不是梦。

@lzjun567
Copy link
Owner

lzjun567 commented May 5, 2017

@Fity 多谢建言

我现在遇到的问题是,如果你现在发了一个PR,单元测试用Mock,那么我如何验证你的代码是否真的可以正确调用知乎的接口呢,比如你写了一个评论接口,能不能真的评论成功,如何判断,还是放任不管,等调用者来发现问题?

另外,你说的在另外的地方验证,那么这个另外的地方是指什么地方,以及它的时机是什么?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants