简体   繁体   English

如何仅运行测试套件中添加的测试用例而不是该类中可用的所有测试用例?

[英]How to run only the test cases added in a test suite and not all the test cases available in the class?

I have written a Test Suite. 我写了一个测试套件。

myTestsuite.py myTestsuite.py

import unittest
from myTestCase2 import MyTestCase2
from prime_num_validation import Prime_Num_Validation

def my_test_suite():
    suite = unittest.TestSuite()
    suite.addTest(MyTestCase2('test_greaterCheck2'))
    #To add only test case: test_greaterCheck2 from the MyTestCase2 class
    suite.addTest(Prime_Num_Validation('test_prime_check'))
    #To add only test case: test_prime_check from the MyTestCase2 class
    return suite

if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    runner.run(my_test_suite())

Now when I run this using command line with: python -m unittest -v myTestsuite , It runs all the test cases from the MyTestCase2 class, which actually has 3 TC's, but we added only one out of 3 in our suite. 现在,当我使用命令行运行: python -m unittest -v myTestsuite ,它运行来自MyTestCase2类的所有测试用例,实际上有3个TC,但我们在套件中只添加了3个。

How should we avoid invoking all test case and executing only those which are present in the suite. 我们应该如何避免调用所有测试用例并仅执行套件中存在的测试用例。

When I run this using Pycharm editor, it again executes all the test cases from MyTestCase2 . 当我使用Pycharm编辑器运行它时,它再次执行MyTestCase2所有测试用例。

You can have the marker on top of your unit test, something call xfail that will skip the test. 您可以在单元测试的顶部放置标记,调用xfail会跳过测试。

for example, 例如,

Below example skip test_function3() 下面的示例跳过test_function3()

import sys

def test_function1():

def test_function2():

@pytest.mark.skipif(sys.version_info < (3,3),
                    reason="requires python3.3")
def test_function3():

For reference please go through this site, you will find more information py.test skipif 如需参考,请浏览本网站,了解更多信息py.test skipif


the link I provided above has example of using xfail marker also. 我上面提供的链接也有使用xfail标记的示例。

You can use the xfail marker or create your own custom marker also. 您也可以使用xfail标记或创建自己的自定义标记。 xfail is to indicate that you expect a test to fail. xfail表示您希望测试失败。

You can run xfail test using following command. 您可以使用以下命令运行xfail test。

pytest --runxfail

As with skipif you can also mark your expectation of a failure on a particular platform: skipif您也可以在特定平台上标记您对失败的期望:

Example

import pytest
xfail = pytest.mark.xfail

@xfail
def test_func1():
    assert 0

@xfail(run=False)
def test_func2():
    assert 0

I also assumed that running python -m unittest -v myTestsuite would execute just the test cases in the defined test suites. 我还假设运行python -m unittest -v myTestsuite只会在定义的测试套件中执行测试用例。 Calling the myTestsuite.py test module directly (ie, not passing module as a parameter to the unittest library module), however, should produce the result you are after. 但是,直接调用myTestsuite.py测试模块(即,不将模块作为参数传递给unittest库模块)应该产生您所追求的结果。 Try running the following: 尝试运行以下内容:

python myTestsuite

NOTE: You will need to pass the "verbosity=1" argument to the TextTestRunner function instead of using "-v" on command line (or modify myTestsuite.py to take a "-v" parameter and pass that to TextTestRunner) 注意:您需要将“verbosity = 1”参数传递给TextTestRunner函数,而不是在命令行上使用“-v”(或修改myTestsuite.py以获取“-v”参数并将其传递给TextTestRunner)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM