我正在尝试执行以下测试套件:
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 运行器结果。
如何仅使用 unitest 运行器从我的套件终端运行?
这个问题的解决方案非常简单,因为我只是误解了我的单元测试是如何一直执行的。
我唯一要做的就是从我的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$