doctest,unittest或if __name __ ='__ main__' [英] doctest, unittest, or if __name__='__main__'

查看:69
本文介绍了doctest,unittest或if __name __ ='__ main__'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于编写测试代码,看起来有三种方式通常可以实现



(1)。使用doctest模块,


(2)。使用unittest模块(即pyunit),或者


(3)。只需输入if __name__ =''__ main __'':"在你的

模块的底部,其中包含通过其步调手动运行课程的代码。


那么,首选哪种方式?在我看来,unittest是要走的路,

至少是因为它让你的测试代码与被测试的代码分开。

如果unittest是编写测试代码的标准方法,为什么我们仍然需要

有doctest? (我注意到在PEP 3000中没有提及弃用

doctest模块)。

解决方案

< blockquote> jo***********@yahoo.com 写道/>

为了编写测试代码,看起来通常有三种方式:

(1)。使用doctest模块,

(2)。使用unittest模块(即pyunit),或者

(3)。只需输入if __name__ =''__ main __'':"在你的
模块的底部,包含代码,以便通过它的步调手动运行你的课程。

那么,首选哪种方式?在我看来,单元测试是最佳选择,至少因为它让你将测试代码与被测试的代码分开。

如果unittest是标准的方法,那么写测试代码,为什么我们还有doctest?


,因为它是一种非常优越的编写测试方式?

(我注意到在PEP 3000中没有提及弃用
doctest模块)。




为什么comp.lang.python突然充满了那些想要弃用任何东西的人不明白吗?不要你的b $ b你有更好的时间吗?


< / F>


jo *********** @ yahoo.com 写道:

如果unittest是编写测试代码的标准方法,为什么我们仍然有doctest? (我注意到在PEP 3000中没有提及弃用
doctest模块)。




因为两者都有其优缺点和权利存在。 Doctest

非常容易使用,你可以一石二鸟。


而不是删除其中一个,我想看到另一个

替换,例如标准库中的py.test,因为unittest确实是标准的,但是笨拙且没有pythonic。


- Christoph


>因为它是一种非常优越的编写测试方式吗?


嗯,因为这是一个邮件列表,其中讨论了这种事情,因为讨论了b $ b我当时询问是哪种方式

首选,你能告诉我们为什么你(想必)认为

doctest远远优于unittest吗?

为什么comp.lang.python突然充满了那些想要弃用他们不理解的东西的人呢?


我没说我想弃用它。我写道,我注意到了

在PEP 3k中没有提及关于弃用它。

你不是更好地处理你的时间吗?




那不是很友好。


无论如何,我不是在攻击doctest,而只是尝试

了解为什么似乎有两种非常相似的方式来测试你的系统内置的代码(考虑到

这里的一个座右铭是应该有一个 - 和

最好只有一个 - 显而易见的方法。)。


For writing testcode, it looks like there''s three ways that it''s
typically done:

(1). using the doctest module,

(2). using the unittest module (i.e. "pyunit"), or else

(3). just putting an "if __name__ = ''__main__'':" at the bottom of your
module containing code to manually run your class through its paces.

So, which way is preferred? Seems to me that unittest is the way to go,
at least because it has you separate your test code from the code being
tested.

If unittest is the standard way to write test code, why do we still
have doctest? (I notice there''s no mention in PEP 3000 of deprecating
the doctest module).

解决方案

jo***********@yahoo.com wrote

For writing testcode, it looks like there''s three ways that it''s
typically done:

(1). using the doctest module,

(2). using the unittest module (i.e. "pyunit"), or else

(3). just putting an "if __name__ = ''__main__'':" at the bottom of your
module containing code to manually run your class through its paces.

So, which way is preferred? Seems to me that unittest is the way to go,
at least because it has you separate your test code from the code being
tested.

If unittest is the standard way to write test code, why do we still
have doctest?
because it''s a vastly superior way to write tests ?
(I notice there''s no mention in PEP 3000 of deprecating
the doctest module).



why is it that comp.lang.python is suddenly full of folks who
want to deprecate anything they don''t understand ? don''t
you have better things to do with your time ?

</F>


jo***********@yahoo.com wrote:

If unittest is the standard way to write test code, why do we still
have doctest? (I notice there''s no mention in PEP 3000 of deprecating
the doctest module).



Because both have their pros and cons and their right to exist. Doctest
is really easy to use and you can kill two birds with one stone.

Rather than removing one of the two, I would like to see yet another
alternatise such as py.test in the standard lib, because unittest is
indeed standard, but clumsy and un-pythonic.

-- Christoph


> because it''s a vastly superior way to write tests ?

Well, since this is a mailing list where this sort of thing is
discussed, and since I was was asking about which way
is preferred, could you tell us why you (presumably) think
doctest is vastly superior to unittest?

why is it that comp.lang.python is suddenly full of folks who
want to deprecate anything they don''t understand ?
I didn''t say I wanted to deprecate it. I wrote that I noticed
there was no mention in PEP 3k about deprecating it.
don''t you have better things to do with your time ?



That wasn''t very friendly.

Anyhow, I''m not attacking doctest, but rather, just trying
to understand why there seems to be two very similar ways
of testing your code built into the system (considering that
one motto around here is "There should be one -- and
preferably only one -- obvious way to do it.").


这篇关于doctest,unittest或if __name __ ='__ main__'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆