【发布时间】:2017-11-06 18:26:05
【问题描述】:
我有一个用 Python 3 编写的 API 包装类 WfcAPI,我想使用 PyUnit 对其进行测试。
WfcAPI 的 setUpClass() 涉及登录到外部 API 服务器。当前的功能测试实现的密码为obfuscated with Base64 encoding,但出于安全原因,这与理想的解决方案相差远。
import unittest
import base64
from pykronos import WfcAPI
class Test(unittest.TestCase):
@classmethod
def setUpClass(self):
password = base64.b64decode("U29tZVBhc3N3b3Jk").decode("utf-8")
self.kronos = WfcAPI("SomeUsername", password)
我希望能够为我的 API 包装器快速运行功能测试,但我还想确保我的用户名和密码不包含在代码中。
如何为需要用户名和密码的操作设置 Python 功能测试?
【问题讨论】:
-
你不能用吗,
password = input('Enter Passowrd') -
@harshil9968 如果我提示用户输入密码,我想用
getpass隐藏输入 -
@StevenVascellaro,你可以使用
pytest代替unittest吗? -
@lmiguelvargasf 切换到 pytest 会做什么?
-
@StevenVascellaro,它将简化您的测试,因为您不需要创建继承自
unittest.TestCase的类,如果是setUp方法,您可以定义一个夹具。最后,在我看来。pytest让测试变得更简单、更愉快 =)。
标签: python python-3.x security functional-testing python-unittest