出于某种原因,我在尝试将命令行参数与我的单元测试一起使用时遇到了问题。
简单地说,我想要的只是在我的所有测试中使用env。我在这里做错了什么?
# -*- coding: utf-8 -*-
import argparse
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import unittest, time, re
class mySmokeTest(unittest.TestCase):
def __init__(self, args):
self.env = args.env
def setUp(self):
self.driver = webdriver.Firefox()
self.base_url = "http://google.com"
self.verificationErrors = []
self.accept_next_alert = True
def test_add_to_cart(self, env):
# open base page
driver = self.driver
driver.get("{0}".format(env))
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('env', default='environment to test against')
args = parser.parse_args()
env = args.env
runner = unittest.TextTestRunner()
itersuite = unittest.TestLoader().loadTestsFromTestCase(mySmokeTest)
runner.run(itersuite)
错误:
self.env = args.env
AttributeError: 'str' object has no attribute 'env'
答案 0 :(得分:0)
我实际上并不知道为什么......但是将主要内容改为:
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('env', default='url to test against')
args = parser.parse_args()
env=args.env
mySmokeTest.env = env
runner = unittest.TextTestRunner()
itersuite = unittest.TestLoader().loadTestsFromTestCase(mySmokeTest)
runner.run(itersuite)
请注意:
mySmokeTest.env = args.env does not work