简体   繁体   English

Selenium python如何使用来自unittest.TestCase的assertEqual方法“没有这样的测试方法 <class 'unittest.case.TestCase'> ”

[英]Selenium python how to use assertEqual method from unittest.TestCase “No such test method in <class 'unittest.case.TestCase'>”

I try to use assertEqual in usual class and can't call method from unittest.TestCase 我尝试在通常的类中使用assertEqual,并且不能从unittest.TestCase调用方法

    class MyPages(unittest.TestCase):

        @classmethod
        def setUpClass(cls):
            basetest.BaseTest().open_browser('firefox')
            basetest.BaseTest().login()


        def testCreateFolder(self):
            print "111111111"

        def testCreateFolder1(self):
            print "222222222"

        @classmethod
        def tearDownClass(cls):
            basetest.BaseTest().close_browser()

And in my BaseTest I want to make login with text assert. 在我的BaseTest中,我想用文本断言进行登录。

    class BaseTest():

        def open_browser(self, browser):
            self.driver = config.browser[browser]
            global driver
            driver = self.driver
            driver.get(config.url)


        def login(self):
            # Go to authorisation page
            driver.find_element_by_xpath(link.header["Login_button"]).click()
            # Get text from LOGIN label and assert it with expected text
            login_text = driver.find_element_by_xpath(link.author_popup["Login_label"])
            login_text.get_attribute("text")
            print login_text.text
            unittest.TestCase().assertEqual(1, 1, "helllllllo")
            unittest.TestCase().assertEqual(login_text.text, text.author_popup["Login"],
                                "Wrong label on log in auth popup. Expected text:")

As a result I have the following: 结果我有以下几点:

    Error
    Traceback (most recent call last):
      File "D:\python\PD_Tests\pages\my_pages.py", line 17, in setUpClass
        basetest.BaseTest().login()
      File "D:\python\PD_Tests\tests\basetest.py", line 25, in login
        unittest.TestCase().assertEqual(1, 1, "helllllllo")
      File "C:\Python27\lib\unittest\case.py", line 191, in __init__
        (self.__class__, methodName))
    ValueError: no such test method in <class 'unittest.case.TestCase'>: runTest

Can I use assertEqual method in my method if my class is not unittest.TestCase? 如果我的类不是unittest.TestCase,我可以在我的方法中使用assertEqual方法吗?

I think there's a way to do what you want, but it's a little bit of a hack. 我认为有办法做你想做的事,但这有点像黑客。

The constructor for the TestCase class takes a method name as a parameter, and this parameter has the default value "runTest" . TestCase类的构造函数将方法名称作为参数,此参数的默认值为"runTest" The docstring for this constructor reads as follows: 此构造函数的docstring如下所示:

Create an instance of the class that will use the named test method when executed. 创建将在执行时使用命名测试方法的类的实例。 Raises a ValueError if the instance does not have a method with the specified name. 如果实例没有具有指定名称的方法,则引发ValueError。

Hopefully this should explain the error message you are seeing. 希望这可以解释您看到的错误消息。

If you want to create a TestCase just to use the assert methods, you can pass in the name of some other method instead, such as __str__ . 如果要创建TestCase只是为了使用assert方法,可以传入其他方法的名称,例如__str__ That will get you past the checking done in the constructor: 这将使您完成构造函数中的检查:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from unittest import TestCase
>>> t = TestCase("__str__")
>>> t.assertEqual(3, 5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\unittest\case.py", line 511, in assertEqual
    assertion_func(first, second, msg=msg)
  File "C:\Python27\lib\unittest\case.py", line 504, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: 3 != 5
>>> t.assertEqual(3, 3)
>>>

As long as you don't try to run your TestCase, this shouldn't be a problem. 只要您不尝试运行TestCase,这应该不是问题。

Is there a special reason not to subclass BaseTest from unittest.TestCase ? 是否有特殊原因unittest.TestCase BaseTest

My solution would have been to have a SeleniumTestcase class, which inherits from unittest.TestCase and provides the setUp and tearDown methods. 我的解决方案是拥有一个SeleniumTestcase类,它继承自unittest.TestCase并提供setUptearDown方法。 Just make sure your config is known when your SeleniumTestcase instance is created. 只需确保在创建SeleniumTestcase实例时知道您的config

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

相关问题 使用unittest.TestCase子类Python的方法调用测试方法 - Invoking Test Methods with a Method for a unittest.TestCase Subclass Python unittest.TestCase的setUp方法是否知道当前测试用例? - Does setUp method from unittest.TestCase know the current test case? 是否可以从 python unittest.TestCase 运行单个测试方法并引用该方法? - Is it possible to run a single test method from a python unittest.TestCase with a reference to the method? 如何在从unittest.TestCase继承的类中创建通用方法? - How do you create a common method within a class that inherited from unittest.TestCase? 使用unittest.TestCase的运行方法 - Using run method of unittest.TestCase 来自unittest.TestCase的tearDownClass的工作流程如何? - How is the workflow of tearDownClass from unittest.TestCase? 在python中重载unittest.testcase - overloading unittest.testcase in python 有没有将自定义/调试消息添加到python / django unittest.TestCase的失败测试方法细节的方法? - Any ways to add custom/debug message to details of failed test method of python/django unittest.TestCase? 验证数组长度相等的Unittest.TestCase方法 - Unittest.TestCase Method to Verify Arrays are of Equal Length 通过命令行从 unittest.TestCase 运行单个测试 - Running a single test from unittest.TestCase via the command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM