如何使用Unittest Python中的1st函数运行第二个函数

时间:2016-01-25 20:55:34

标签: python email selenium send python-unittest

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

我正在使用Selenium运行Python Unittest发送的gmail测试。该过程在SetUp上运行后,它运行第一个函数testLoginEmail,它可以成功登录我的gmail帐户。然后我想继续运行第二个函数testComposeEmail,它应该在第一个函数之后运行,因为它需要单击“Compose”按钮。但它无法运行。

有人可以帮助修改如何运行第二个功能的代码吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

注意:我没有尝试过您的代码,但可能存在两个问题。

问题1:我认为unittest函数(setUp和tearDown除外)按字母顺序运行,不一定是它们的顺序.testComposeEmail将在testLoginEmail之前运行。应该很容易用print语句进行测试。这可以通过明智的重命名来修复,例如test_1_LoginEmail和test_2_ComposeEmail。

无论其

问题2:在EACH测试之前和之后运行setUp和tearDown,而不是整套测试,因此修复问题1可能无济于事。我认为测试应该写成完全相互独立。

您可以将两个测试合并为一个单一测试,看看是否有效。

相关问题