我有一堆由test_xyz.py
中的类组织的测试用例
我必须通过连接到远程服务器的连接来运行这些测试(每个测试用例将在上述连接上进行交易(。
我必须多次重复相同的测试(每次使用不同的连接凭据(。目前,我每次都手动更改凭据并重新进行测试。
我可以将存储在字典中的凭据。但是,如何自动化测试以一次又一次地迭代字典中存储的凭据?
根据我阅读的内容,固定装置可能会有所帮助吗?但是我不确定。
任何建议都赞赏。预先感谢。
test_xyz.py示例
conn = remote_conn(uname, pwd)
class Test_123:
"""Test Class"""
@pytest.mark.parametrize(...)
def test_1(self):
assert conn.do_this()
@pytest.mark.parametrize(...)
def test_2(self):
assert conn.do_that()
尝试对固定装置进行参数。我建议为您的RemoteConnection
创建固定装置,然后将其传递给测试。这是您可以做到的。
import pytest
class RemoteConnection:
def __init__(self, u, p):
self.u = u
self.p = p
# print "{}-{}".format(u, p)
def close(self):
# print "closed"
pass
def do_this(self, a):
# print "do_this {}".format(a)
return True
def do_that(self, a):
# print "do that {}".format(a)
return True
credentials = [("uname1", "pwd1"), ("uname2", "pwd3"), ("uname3", "pwd3")]
@pytest.fixture(scope="class", params=credentials)
def conn(request):
_credentials = request.param
con = RemoteConnection(*_credentials)
def fin():
con.close()
request.addfinalizer(fin)
return con
class Test_123:
"""Test Class"""
@pytest.mark.parametrize("a", [1, 2])
def test_1(self, a, conn):
assert conn.do_this(a)
@pytest.mark.parametrize("a", [1, 2])
def test_2(self, a, conn):
assert conn.do_that(a)
和
的样品输出plugins: xdist-1.11, profiling-1.2.6, cov-1.6
collecting ... collected 12 items
../../../../../../Library/Preferences/PyCharm2017.1/scratches/scratch_8.py::Test_123::test_1[conn0-1] PASSED
../../../../../../Library/Preferences/PyCharm2017.1/scratches/scratch_8.py::Test_123::test_1[conn0-2] PASSED
../../../../../../Library/Preferences/PyCharm2017.1/scratches/scratch_8.py::Test_123::test_2[conn0-1] PASSED
../../../../../../Library/Preferences/PyCharm2017.1/scratches/scratch_8.py::Test_123::test_2[conn0-2] PASSED
../../../../../../Library/Preferences/PyCharm2017.1/scratches/scratch_8.py::Test_123::test_1[conn1-1] PASSED
../../../../../../Library/Preferences/PyCharm2017.1/scratches/scratch_8.py::Test_123::test_1[conn1-2] PASSED
../../../../../../Library/Preferences/PyCharm2017.1/scratches/scratch_8.py::Test_123::test_2[conn1-1] PASSED
../../../../../../Library/Preferences/PyCharm2017.1/scratches/scratch_8.py::Test_123::test_2[conn1-2] PASSED
../../../../../../Library/Preferences/PyCharm2017.1/scratches/scratch_8.py::Test_123::test_1[conn2-1] PASSED
../../../../../../Library/Preferences/PyCharm2017.1/scratches/scratch_8.py::Test_123::test_1[conn2-2] PASSED
../../../../../../Library/Preferences/PyCharm2017.1/scratches/scratch_8.py::Test_123::test_2[conn2-1] PASSED
../../../../../../Library/Preferences/PyCharm2017.1/scratches/scratch_8.py::Test_123::test_2[conn2-2] PASSED
================= 12 passed, 2 pytest-warnings in 0.08 seconds =================
Process finished with exit code 0