【问题标题】:How to call a test method from another test in unittest framework?如何从 unittest 框架中的另一个测试调用测试方法?
【发布时间】:2017-03-20 10:30:23
【问题描述】:

我已经编写了类似于以下代码的代码来实现我的要求,但是我收到了错误消息。

class Test(BaseSetup):

    def test_01_create_record(self):        
        --
    def test_02_edit_record(self):
        --
    def test_03_delete_record(self):   
        --
    def test_04_verify_item_tab(self):
        testObj=Test()
        testObj.test_01_create_record()
        do this....
        testObj.test_03_delete_record()        

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

以上三个测试方法(test_01、test_02 和 test_03)都运行良好,但最后一个测试,即 test_04 失败。它无法使用 test_01 创建记录(尽管它单独运行良好)。我上次测试时收到以下错误消息。

self.driver.find_element_by_xpath(self.content_tab_xpath).click()
AttributeError: 'NoneType' object has no attribute 'find_element_by_xpath'

上面的错误信息是针对第一个测试(test_01_create_record)的,只有当我从另一个测试中调用第一个测试方法时才会得到,但是当我单独运行它时效果很好。知道我可能会错过什么吗?非常感谢

【问题讨论】:

  • 你为什么要在它自己的方法中创建一个新的类实例?你觉得self是什么?
  • 哦,太好了,我只需要调用 self.test_01_create_record()。无需在这里创建类的实例。非常感谢

标签: python unit-testing selenium


【解决方案1】:

不需要在里面创建你的类的实例,只需用self引用实例即可:

def test_04_verify_item_tab(self):
    self.test_01_create_record()
    self.test_03_delete_record()

【讨论】:

    猜你喜欢
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多