开发者

UnitTest in Python [duplicate]

开发者 https://www.devze.com 2023-03-14 17:33 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: ValueError: no such test method in <class 'myapp.tests.SessionTestCase'>: runTest
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

ValueError: no such test method in <class 'myapp.tests.SessionTestCase'>: runTest

import unittest

class BzTestSe(unittest.TestCase):
    DEFAULTUSERNAME = 'username-a2'
    DEFAULTPASSWORD = 'pass'
    DEFAULTHOST = 'localhots'

    def __init__(self,username=DEFAULTUSERNAME, password=DEFAULTPASSWORD, host=DEFAULTHOST):
        super(unittest.TestCase,self).__init__()
        self.username=username
        self.password=password
        self.host=host

class test_se_topwebsite(BztTestSe):
    def setUp(self):
        print "setup"

    def test_test_se_topwebsite(self):
        self.fail()

When I call the above class from another file,I get the开发者_JAVA技巧 following error. Please let me know where I am wrong.

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "testsuite/test_se.py", line 10, in __init__
    super(unittest.Testcase,self).__init__()
File "/usr/lib/python2.7/unittest/case.py", line 184, in __init__
    (self.__class__, methodName))
ValueError: no such test method in <class 'testsuite.test_se.BztTestSe'>: runTest


Lets try and go back to something simple. In using unittest you have a couple of ways to execute your test cases but the simplest way is to have a main function in the file that contains your unittests.

For example:

import unittest

class TestSomething(unittest.TestCase):

    def setUp(self):
        self.message = "does this work"

    def test_message_is_expected(self):
        self.assertEquals("does this work", self.message)


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

Note your test cases (classes) sub class unittest.TestCase, you then use a setUp method to set any state for your test cases, and finally you'll want some methods prefixed test_ ... which the test runner will execute.

If you saved the above file into lets say test_something.py and then in a console ran python test_something.py you'd see the results of the test output to the console.

If you can re cast your example into something a little clearer using this pattern rather than the inheritance hierarchy you've used you may be able to execute your tests.

I realise this is more of a comment than an answer but I can't yet make comments.

0

精彩评论

暂无评论...
验证码 换一张
取 消