以下程序:单元测试在Python
import unittest
class my_class(unittest.TestCase):
def setUp(self):
print "In Setup"
self.x=100
self.y=200
def test_case1(self):
print "-------------"
print "test case1"
print self.x
print "-------------"
def test_case2(self):
print "-------------"
print "test case2"
print self.y
print "-------------"
def tearDown(self):
print "In Tear Down"
print " "
print " "
if __name__ == "__main__":
unittest.main()
给人的输出:
>>> ================================ RESTART ================================
>>>
In Setup
-------------
test case1
100
-------------
In Tear Down
.In Setup
-------------
test case2
200
-------------
In Tear Down
.
----------------------------------------------------------------------
Ran 2 tests in 0.113s
OK
>>>
>>>
问题:
什么意思:
if __name__ == "__main__": unittest.main()
?为什么我们有双下划线前缀为
name
和main
?将在何处创建
my_class
的对象?
看到这个问题http://stackoverflow.com/questions/419163/what-does-if-name-main-do – HavelTheGreat 2015-02-10 18:08:34
1.一次提出一个问题。 2.前导双尾下划线用于指示*“魔术”对象或属于用户控制的命名空间“*,[PEP-0008](https://www.python.org/dev/peps/ PEP-0008 /#描述命名的样式)。这可以防止它们与用户的名字发生冲突(例如,您可以调用函数'main'或变量'name')。见例如http://stackoverflow.com/q/19216895/3001761 3.你是什么意思*“where”* ?! – jonrsharpe 2015-02-10 18:12:41