gpt4 book ai didi

python - 使用 pytest 和 unittest runner 从两个终端运行测试套件,但仅显示 unittest runner 结果并执行一次

转载 作者:行者123 更新时间:2023-11-28 20:36:42 30 4
gpt4 key购买 nike

我正在尝试执行我的以下测试套件:

import unittest
from Login_Page import LoginPageAndLogout


def test_suite():
# get all tests from classes
login_test = unittest.TestLoader().loadTestsFromTestCase(LoginPageAndLogout)

# create a test suite
all_tests = unittest.TestSuite([
login_test
])

# run the suite
unittest.TextTestRunner(verbosity=2).run(all_tests)

从 Pycharm 的终端使用命令:

sudo pytest selenium-tests/testSuite.py -vvv -s

部分输出如下:

============================================================================================================ test session starts ============================================================================================================
platform linux2 -- Python 2.7.14, pytest-3.1.3, py-1.4.34, pluggy-0.4.0 -- /usr/bin/python
cachedir: .cache
rootdir: /home/osboxes/PycharmProjects/WebTesting, inifile:
plugins: cov-2.5.1
collected 3 items

selenium-tests/testSuite.py::LoginPageAndLogout::test_failed_login <- selenium-tests/Login_Page.py PASSED
selenium-tests/testSuite.py::LoginPageAndLogout::test_login <- selenium-tests/Login_Page.py FAILED
selenium-tests/testSuite.py::test_suite test_failed_login (Login_Page.LoginPageAndLogout) ... ok
test_login (Login_Page.LoginPageAndLogout) ... ok

----------------------------------------------------------------------
Ran 2 tests in 55.993s

我的 Login_Page.py 文件的结构是:

class LoginPageAndLogout(unittest.TestCase):

def setUp(self):
# ...

# login with incorrect credentials to get error message
def test_failed_login(self):
# ...

# login with correct credentials
def test_login(self):
# ...

def tearDown(self):
# ...

正如您从输出中看到的那样,我有 2 个测试,但终端反而收集了三个东西,并且每个测试运行两次。有没有办法只执行 PASSED/FAILED 执行,而不执行 ... ok

如果我注释掉 unittest.TextTestRunner(verbosity=2).run(all_tests) 我的测试只执行了一次但是我得到了 ... ok 结果而不是我想要的 PASSED/FAILED;所以我看到的是 pytest 执行结果,而不是 unittests runner 结果。

如何仅使用 unitest runner 从终端运行我的套件?

最佳答案

这个问题的解决方案非常简单,因为我一直误解了我的单元测试是如何执行的。

我唯一要做的就是从我的 testSuite.py 文件中注释掉整个 test_suite 类,然后在该文件的顶部导入类我想执行的测试脚本。

现在我的测试只运行一次,我仍然可以一次执行所有脚本,而无需使用完全相同的命令在我的命令中一个一个地键入它们:sudo pytest selenium-tests/testSuite.py -vvv -s

现在该命令的输出是:

osboxes@osboxes:~/PycharmProjects/WebTesting$ sudo pytest selenium-tests/testSuite.py -vvv -s
========================================================================================================================= test session starts ==========================================================================================================================
platform linux2 -- Python 2.7.14, pytest-3.1.3, py-1.4.34, pluggy-0.4.0 -- /usr/bin/python
cachedir: .cache
rootdir: /home/osboxes/PycharmProjects/WebTesting, inifile:
plugins: cov-2.5.1
collected 2 items

selenium-tests/testSuite.py::LoginPageAndLogout::test_failed_login <- selenium-tests/Login_Page.py PASSED
selenium-tests/testSuite.py::LoginPageAndLogout::test_login <- selenium-tests/Login_Page.py PASSED

====================================================================================================================== 2 passed in 58.81 seconds =======================================================================================================================
osboxes@osboxes:~/PycharmProjects/WebTesting$

关于python - 使用 pytest 和 unittest runner 从两个终端运行测试套件,但仅显示 unittest runner 结果并执行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52078823/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com