Default skip test unless command line parameter present in py.test

前端 未结 2 1651
不思量自难忘°
不思量自难忘° 2021-02-19 01:38

I have a long run test, which lasts 2 days, which I don\'t want to include in a usual test run. I also don\'t want to type command line parameters, that would deselect it and ot

相关标签:
2条回答
  • 2021-02-19 02:21

    try to decorate your test as @pytest.mark.longrun

    in your conftest.py

    def pytest_addoption(parser):
        parser.addoption('--longrun', action='store_true', dest="longrun",
                     default=False, help="enable longrundecorated tests")
    
    def pytest_configure(config):
        if not config.option.longrun:
            setattr(config.option, 'markexpr', 'not longrun')
    
    0 讨论(0)
  • 2021-02-19 02:37

    Alternatively to the pytest_configure solution above I had found pytest.mark.skipif.

    You need to put pytest_addoption() into conftest.py

    def pytest_addoption(parser):
        parser.addoption('--longrun', action='store_true', dest="longrun",
                     default=False, help="enable longrundecorated tests")
    

    And you use skipif in the test file.

    import pytest
    
    longrun = pytest.mark.skipif(
          not pytest.config.option.longrun,
          reason="needs --longrun option to run")
    
    def test_usual(request):
        assert false, 'usual test failed'
    
    @longrun
    def test_longrun(request):
        assert false, 'longrun failed'
    

    In the command line

    py.test
    

    will not execute test_longrun(), but

    py.test --longrun
    

    will also execute test_longrun().

    0 讨论(0)
提交回复
热议问题