python unittest中的属性错误

时间:2015-03-30 07:37:56

标签: python selenium-webdriver phantomjs

执行此单元测试时

AttributeError:'BaseUnit'对象没有属性'driver'

import unittest
import redis
from selenium import webdriver
redis = redis.Redis(host='abc', port='123')
keys = redis.keys('*')
raw_baseunit = redis.get('test:baseunit')`enter code here`

class BaseUnit(unittest.TestCase):
        def setup(self):
                self.driver = webdriver.PhantomJS()

        def test(self):
                self.driver.get("myurl")
                self.driver.find_element_by_id('username').send_keys("ngeo_pur1")
                self.driver.find_element_by_id('password').send_keys("anything")
                self.driver.find_element_by_xpath('html/body/div[1]/div[3]/div/section/div/form/ul/li[5]/div[2]/div/input').click()
                self.driver.get("url")
                self.driver.find_element_by_partial_link_text("18757424").click()
                self.driver.find_element_by_xpath(".//*[@id='tabs']/nav/ul/li[2]/a/i").click()
                Actual = self.driver.find_element_by_xpath(".//*[@id='subcat_baseModelSection.baseModelChoice']/div/div[1]").text

                keys = redis.keys('*')
                raw_baseunit = redis.get('test:baseUnit')
                print "Actual Base Unit=",Actual
                print "Expected Base Unit=",raw_baseunit
                self.assetEquals(raw_baseunit,Actual)

        def teardown(self):
                self.driver.quit()

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

尝试更改类名'BaseUnit'以及

2 个答案:

答案 0 :(得分:0)

尝试将安装程序类的名称修改为此。我刚刚阅读了doc,而setup类是用U大写的

编写的
def setUp(self):
        self.driver = webdriver.PhantomJS()

答案 1 :(得分:0)

您要在unittest.TestCase中覆盖的方法是setUptearDown(请注意首都" U"以及" D"),不是setupteardown

在测试方法运行之前,你的全小写setup方法没有被调用(如果它被调用的话),所以当self.driver属性不存在时test属性不存在{ {1}}方法尝试使用它。