Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a test method from another test in unittest framework?

I have written code something similar to below code to achieve my requirement, but I am getting error message.

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()

Here all above three test method(test_01, test_02 and test_03) runs well, but last test, i.e test_04 fails. It is not able to create the record using test_01(though it works well separately). I am getting below error message for my last test.

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

The above error message is for first test(test_01_create_record), I get only when I call first test method from another test, but when I runs it separately it works well. Any idea what I might be missing? Many thanks

like image 202
Shoaib Akhtar Avatar asked Aug 31 '25 16:08

Shoaib Akhtar


1 Answers

No need to create instance of your class inside it, just reference to the instance with self:

def test_04_verify_item_tab(self):
    self.test_01_create_record()
    self.test_03_delete_record()
like image 185
Iron Fist Avatar answered Sep 02 '25 05:09

Iron Fist