简体   繁体   English

Python unittest没有运行

[英]Python unittest not running

Windows XP Python 2.7 Windows XP Python 2.7

I'm following the code in Beginning Python book and have two files in a folder called testing. 我正在关注Beginning Python一书中的代码,并在名为testing的文件夹中有两个文件。 I'm trying to get it to fail but it wont even run the tests.The first file my_math.py is just a dummy product function 我试图让它失败,但它甚至不会运行测试。第一个文件my_math.py只是一个虚拟产品函数

def product(x, y):
    pass

The second is the test test_my_math.py 第二个是测试test_my_math.py

import unittest, my_math

class ProductTestCase(unittest.TestCase):

    def testIntegers(self):
        for x in xrange(-10, 10):
            for y in xrange(-10, 10):
                p = my_math.product(x, y)
                self.failUnless(p == x*y, 'Integer multiplication failed')

    def testFloats(self):
        for x in xrange(-10, 10):
            for y in xrange(-10, 10):
                x = x/10.0
                y = y/10.0
                p = my_math.product(x, y)
                self.failUnless(p == x*y, 'Float multiplicaton failed')

    if __name__ == '__main__': unittest.main()

When I run the test in the command line 当我在命令行中运行测试时

C:\Python27\Example_Programs\testing>python test_my_math.py

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

C:\Python27\Example_Programs\testing>

Then unindent that if to the top level (no spaces before it). 然后unindent, if到顶层(在它之前没有空格)。 Otherwise it is part of the code block of the class definition and will be executed before the class is finished (thus no unit tests have been created at this point). 否则它是class定义的代码块的一部分,并将在类完成之前执行(因此此时没有创建单元测试)。

The upper reason that Mr. Alfe answered is also correct Other reason might be following Alfe先生回答的最主要原因也是正确的其他原因可能如下

def setUp(self):
        self.browser = webdriver.Firefox()
        browser=self.browser
        browser.get("http://google.com")

It might be possible that you have to probably define this code till browser.get method in setUp() function The rest of the code will be defined in the next segment of the second function 您可能需要定义此代码,直到setUp()函数中的browser.get方法其余代码将在第二个函数的下一个段中定义

& here setUp() function name is mandatory , otherwise it causes an error &这里setUp()函数名是必需的,否则会导致错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM