问题
Getting object has no attribute 'assertEqual' when I try to use the below statement in my code:
self.assertEqual("IRELAND INSTITUTE OF PITTSBURGH", driver.find_element_by_id("cname").get_attribute("value"))
This works fine when I use in my test case instead. I understand that it is a good practice to put asserts in the test case. But I would like to understand why this piece of code cannot work in the page instead if we are following page object pattern
回答1:
assertEqual is a method that belongs to the class TestCase from the unittest module (python unittest docs).
The reason it works in your test code is most likely because you test class inherits from the TestCase class.
class YourTestClass(unittest.TestCase):
pass
You could just use the keyword assert:
assert "IRELAND INSTITUTE OF PITTSBURGH" == driver.find_element_by_id("cname").get_attribute("value")
If the assertion fails, it will raise an AssertionError.
来源:https://stackoverflow.com/questions/42859577/object-has-no-attribute-assertequal