我有一些网址列表,我想为其编写一个小型的Smoke测试。
只需加载网址并断言标题即可确保它们还活着。这可能吗?类标识(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
driver = self.driver
self.driver.implicitly_wait(30)
@data('https://idsvr-test.sc.com/account/signin','https://launchpad-test.sc.com/#&ui- state=dialog','https://ambassador-test.sc.com/')
def test_login_identity(self,url):
self.driver.get("https://idsvr-test.sc.com/account/signin")
self.assertIn("Username / Password Sign In", self.driver.title)
elemU = self.driver.find_element_by_name("UserName").send_keys(User)
elemP = self.driver.find_element_by_name("Password").send_keys(sDecryptPassword)
self.driver.find_element_by_id("login").click()
try:
self.assertEqual("MyTitle", self.driver.title)
print("I asserted the Title")
except Exception as e:
print(e)
try:
self.driver.get("https://launchpad-test.sc.com/#&ui-state=dialog")
self.assertIn("Launch Pad",self.driver.title)
ltitle = self.driver.title
print( "New page" + ltitle)
except Exception as e:
print(e)
try:
self.driver.get("https://ambassador-test.sc.com/")
self.assertIn("SC: Ambassador",self.driver.title)
atitle = self.driver.title
print(atitle)
except Exception as e:
print(e)
如果名称 ==" 主要": unittest.main()
答案 0 :(得分:0)
这看起来像是一个数据驱动的测试。在这种情况下,ddt
包可以帮助您轻松实现:
DDT(数据驱动测试)允许您将一个测试用例乘以 使用不同的测试数据运行它,并使其显示为多个 测试案例。
from ddt import ddt, data
@ddt
class identity(unittest.TestCase):
...
@data('url1', 'url2', 'url3')
def test_login_identity(self, url):
self.driver.get(url)
# ...
应用@ddt
和@data
装饰器基本上会为@data
内传递的每个网址自动生成测试方法。在此示例中,unittest
运行程序将执行3种测试方法。