python - 如何将参数从模块传递到单元测试模块?
问题描述
我有 2 个文件:
test_1.py:
import unittest
class TestMe(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.name = "test"
cls.password = "1234"
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_user_pass(self):
print(self.name)
print(self.password)
if __name__ == '__main__':
unittest.main()
test_2.py:
import unittest
import test_1
import sys
a = sys.argv
if a[1] == '2':
suite=unittest.TestLoader().loadTestsFromModule(test_1)
unittest.TextTestRunner(verbosity=2).run(suite)
我想将参数传递给 test_1(unittes 模块),但我需要将此参数传递给 setUpClass。我怎样才能做到这一点?
谢谢!!!
解决方案
尝试这个...
test_1.py:
import unittest
from test_2 import b
class TestMe(unittest.TestCase):
e = b
@classmethod
def setUpClass(cls):
cls.name = "test"
cls.password = "1234"
cls.parameter = cls.e
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_user_pass(self):
print(self.name)
print(self.password)
print(self.parameter)
if __name__ == '__main__':
unittest.main()
test_2.py:
import unittest
import test_1
import sys
a = sys.argv
b = ""
if a[1] == '2':
b = a[1]
suite = unittest.TestLoader().loadTestsFromModule(test_1)
unittest.TextTestRunner(verbosity=2).run(suite)
我希望这能帮到您。
推荐阅读
- javascript - 在 Jest 中设置文档选择
- css - flexbox 移除 flex-wrap 上的垂直空间
- rabbitmq - Spring Cloud Stream 3.1.2 RabbitMQ 消费者不工作
- javascript - 如何在模块内找到请求的导出?
- javascript - 如何进入 Shadowroot 中的班级
- javascript - 如何将浏览器中加载的图像转换为base64字符串?
- java - java.lang.RuntimeException:无法实例化活动。Java 类未找到异常
- java - 如何将 Gson 与番石榴表一起使用
- python - 我怎样才能扩大和匹配这些情节?
- html - 如何从jquery附加标签内部?