如何在UnitTestPython中使用第一个函数运行第二个函数

如何在UnitTestPython中使用第一个函数运行第二个函数,python,email,selenium,send,python-unittest,Python,Email,Selenium,Send,Python Unittest,我正在用Selenium通过Python Unittest运行这个send gmail测试。该进程在安装程序上运行后,将运行第一个函数TestLoginMail,它可以成功登录到我的gmail帐户。然后我想继续运行第二个函数testComposeEmail,它应该在第一个函数之后运行,因为它需要单击“撰写”按钮。但它无法运行 有人能帮我修改一下如何运行第二个函数的代码吗?非常感谢 注意:我还没有试过你的代码,但是可能有两个问题 问题1:我相信unittest函数(除了setUp和tearDown

我正在用Selenium通过Python Unittest运行这个send gmail测试。该进程在安装程序上运行后,将运行第一个函数TestLoginMail,它可以成功登录到我的gmail帐户。然后我想继续运行第二个函数testComposeEmail,它应该在第一个函数之后运行,因为它需要单击“撰写”按钮。但它无法运行


有人能帮我修改一下如何运行第二个函数的代码吗?非常感谢

注意:我还没有试过你的代码,但是可能有两个问题

问题1:我相信unittest函数(除了setUp和tearDown)是按字母顺序运行的,不一定是按字母顺序运行的。testComposeEmail将在testLoginEmail之前运行。应该很容易用打印语句进行测试。这可以通过明智的重命名来解决,例如test_1_LoginEmail和test_2_ComposeEmail

然而

问题2:安装和拆卸在每个测试之前和之后运行,而不是整个测试套件,因此修复问题1可能没有帮助。我认为测试应该是完全独立的


您可以将这两个测试组合成一个整体测试,看看是否有效。

您有什么错误?通常要求测试顺序会导致糟糕的测试设计,但如果您知道自己在做什么,您可以按字母顺序命名它们,或者使用与相同的功能
username = "robertredrain@gmail.com"
password = ""
tomailid = "robertredrain@yahoo.com"
emailsubject = "robertredrain@yahoo.com"
mailbody = "Great! you sent email:-)" + "\n" + "Regards," + "\n" + "Robert"

class send_email(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.baseUrl = "http://mail.google.com/intl/en/mail/help/about.html"

    def tearDown(self):
        self.driver.close();

    def testLoginEmail(self):
        self.driver.get(self.baseUrl)
        self.driver.maximize_window()
        self.driver.find_element_by_id("gmail-sign-in").click()
        self.driver.find_element_by_id("Email").clear()
        self.driver.find_element_by_id("Email").send_keys(username)
        self.driver.find_element_by_id("next").click()
        time.sleep(5)
        self.driver.find_element_by_id("Passwd").clear()
        self.driver.find_element_by_id("Passwd").send_keys(password)
        self.driver.find_element_by_id("signIn").click()

        #Verify login
        if "Gmail" in self.driver.title:
            print("Logged in sucessfully !!!" + self.driver.title)
        else:
            print("Unable to loggin :-( " + self.driver.title)

        time.sleep(5)


    def testComposeEmail(self):
        self.driver.find_element_by_xpath("//div[text()='COMPOSE']").click()
        time.sleep(5)
        self.driver.find_element_by_class_name("vO").send_keys(tomailid)
        self.driver.find_element_by_class_name("aoT").send_keys(emailsubject)

        self.driver.find_element_by_class_name("Am").clear()
        self.driver.find_element_by_class_name("Am").send_keys(mailbody)
        self.driver.find_element_by_xpath("//div[text()='Send']").click()



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