运行pytest和unittest测试套件,仅显示unittest运行器结果并在终端上执行

2024-09-27 23:19:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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的终端使用命令:

^{pr2}$

输出的一部分如下:

============================================================================================================ 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):
        # ...

从输出中可以看到,我有两个测试,但是终端收集了三个东西,并且每个测试运行两次。有没有办法只执行PASSED/FAILED执行,而不是... ok?在

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

我怎样才能只使用unitest runner从终端运行我的套件?在


Tags: runpytestselfdefseleniumpagelogin
1条回答
网友
1楼 · 发布于 2024-09-27 23:19:49

解决这个问题的方法很简单,因为我一直都误解了我的单元测试是如何执行的。在

我唯一要做的就是从我的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$ 

相关问题 更多 >

    热门问题