3

Windows XP Python 2.7

I'm following the code in Beginning Python book and have two files in a folder called 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

def product(x, y):
    pass

The second is the test 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>
2
  • 4
    Presumably the indentation of the if __name__ test is a posting error? Commented Jul 3, 2013 at 11:56
  • No, it's not. That's the exact output from having this indented that way. Commented Jul 3, 2013 at 12:03

2 Answers 2

2

Then unindent that if to the top level (no spaces before it). 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).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks The indented If was causing it not to run. Another Homer moment
0

The upper reason that Mr. Alfe answered is also correct Other reason might be following

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

& here setUp() function name is mandatory , otherwise it causes an error

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.