将参数传递给python unittest
我有一个函数测试“y1.py”,我正试图从python/django函数中向它传递参数。在调用函数中,我有:将参数传递给python unittest,python,Python,我有一个函数测试“y1.py”,我正试图从python/django函数中向它传递参数。在调用函数中,我有: import unittest, sys import ft1.y1 ft1.y1.testVars = [1, 2, 3, "foo"] unittest.main(module=ft1.y1, argv=sys.argv[:1], exit=False) 基于h。ttp://stackoverflow.com/questions/2812132/how-to-pass-variab
import unittest, sys
import ft1.y1
ft1.y1.testVars = [1, 2, 3, "foo"]
unittest.main(module=ft1.y1, argv=sys.argv[:1], exit=False)
基于h。ttp://stackoverflow.com/questions/2812132/how-to-pass-variables-using-unittest-suite
y1.py:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
class Y1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.yahoo.com/"
self.verificationErrors = []
self.accept_next_alert = True
print('tvars' +self.testVars )
....................
if __name__ == "__main__":
unittest.main()
我得到:
Traceback (most recent call last):
File "F:\envs\r1\driver1\ft1\y1.py", line 17, in setUp
print('tvars '+ y1.testVars )
AttributeError: type object 'y1' has no attribute 'testVars'
----------------------------------------------------------------------
Ran 1 test in 2.330s
FAILED (errors=1)
[02/Feb/2014 23:59:42] "GET /runtest/ HTTP/1.1" 200 7
我怎样才能解决这个问题
编辑:
根据建议,我将行更改为:
print('tvars' + sys.module[__name__].testVars )
我得到:
E
======================================================================
ERROR: test_y1 (ft1.y1.y1)
----------------------------------------------------------------------
Traceback (most recent call last):
File "F:\envs\r1\driver1\ft1\y1.py", line 17, in setUp
print('tvars' + sys.module[__name__].testVars )
AttributeError: 'module' object has no attribute 'module'
----------------------------------------------------------------------
Ran 1 test in 2.981s
FAILED (errors=1)
如果您试图引用模块而不是类(您的
testVars
处于module级别),您可能应该使用sys.modules[\uu\u名称].testVars
,这使得:
print('tvars' +self.testVars )
成为:
print('tvars' + sys.modules[__name__].testVars )
你可能真的是指
sys.modules
?多元化?多亏了你们两位,我使用了print('tvars'+str(sys.modules[name].testVars)),而且效果很好