[英]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
并提供setUp
和tearDown
方法。 Just make sure your config
is known when your SeleniumTestcase
instance is created. 只需确保在创建
SeleniumTestcase
实例时知道您的config
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.