向python单元传递参数

2024-10-02 00:37:14 发布

您现在位置: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-variables-using-unittest-suite在

y1.py:

^{pr2}$

我得到了:

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)

Tags: 函数inpytestimportsysunittestmodule
1条回答
网友
1楼 · 发布于 2024-10-02 00:37:14

如果您试图引用模块而不是类(您的testVars模块级别),您可能应该使用sys.modules[__name__].testVars,这使得:

print('tvars' +self.testVars )

成为:

^{pr2}$

相关问题 更多 >

    热门问题