Capture KeyboardInterrupt in pytest in order to Skip Tests - pytest

I was wondering if I can skip test during it's execution using KeyboardInterrupt.
I saw that pytest got the hook function:
def pytest_keyboard_interrupt(excinfo)
which is great, but after it's running, it stops the execution of all the tests, and I want only to skip the specific test that the KeyboardInterrupt came from.
EDIT: I was trying to call pytest from another file and catching KeyboardInterrupt like that:
import pytest
try:
pytest.main(["begin.py", "-vs", "--pdb"])
except KeyboardInterrupt:
pytest.skip("Skipped")
But it doesn't work.
I will appreciate any help :)

Related

Print available command line options using pytest

I need to print the command line options using pytest and I am following this thread: How can I see the current pytest configuration?
The code snippet mentioned works. However, this gives all the pytest options as well. I want to print just the command line options which are available in the code (added by using pytest_addoption). Is there a way to do this?
Thanks
# test_sample.py
import pytest
import os
def test_answer(pytestconfig):
for item_name in dir(pytestconfig.option):
if not item_name.startswith('_'):
print(f'{item_name}: {getattr(pytestconfig.option,item_name)}')
assert 0 # to see what was printed

Is there a way in pytest to generate test report from custom file for non-python test cases?

Background
Trigger legacy non-python testcases from pytest. Since these testcases are categorized as testsuites, from pytest perspective we'll be doing an ssh on a remote machine and trigger a testsuite. So from pytest's point of view it is a single testcase, but actually it would be a bunch executing on remote machine.
Requirement
The testsuite will generate a testreport which we'll SCP back to the pytest machine. I wish to parse the testreport and report the PASS/FAIL for each testcase from pytest
I have been looking into example but still can't get my head around on how would I trigger the test case with SSH and parse the testreport(XML/JSON) and generate pytest report
Any suggestions ?
Update:
I have been able to parse the yaml file to generate the terminal report(pytest_terminal_summary) for my testcases. But I would like that pytest also reports the number of testcases failed/passed.
Can you try pytest test.py -v --junitxml="result.xml"
You can also generate html result using pytest file.py -sv --html report.html

How do I show the exact command being tested when a pytest script runs?

How is it possible that anyone uses pytest without having it output the exact command that it runs.
I have a set of 5 test scripts with a total of 41 different test combinations. The script functions all basically follow this same template where at some stage, the funciton does:
subprocess.Popen(cmdline_builder(opt, name, options))
When there's a failure, the output is nearly useless, it doesn't show the exact command that was run.
How does anybody use this ? How would you expect to debug a failed test without knowing what to run ?

Currently testing file name in pytest [duplicate]

What I'm trying to do
I'm writing a small framework in python using pytest, and as part of the teardown I am taking a screenshot.
Now, I want that screenshot to be named according to the running test, not conftest.py
So, for example, my code right now is:
driver.save_screenshot(os.path.basename(__file__)+'.png')
The problem
This prints the name "conftest.py".
I want to print the calling test name if possible. I am guessing using requests?
You can access the current file path via request.node.fspath. Example:
# conftest.py
import pytest
#pytest.fixture
def currpath(request):
return str(request.node.fspath)

Running a test with tox based on a keyword

I am using pytest with tox. I can run some of my tests with a keyword like this:
pytest -k <keyword> path/to/tests
Now it would be really convenient to be able to do this also with tox, as the environments there are clean and different python versions can be tested. However the nearest thing I have found is:
tox -- path/to/tests/test_very_specific_name.py:TestClass.test_func
This is not easy to type, so I rather just run tox without arguments and wait 2 minutes for everything to finish.
Is there a way to run single tests based on keywords with tox? I tried:
tox -- -k <keyword>
This results in a huge list of import errors. It doesn't seem to be able to find any of my local includes. Is this supposed to work?
I figured it out thanks to the comment by phd.
Everything on the command line after -- can be used in tox.ini as {posargs}. I was using that wrong. My tox.ini now has a line like this:
commands = py.test {posargs} <test_folder>
Now it works perfectly with:
tox -- -k <keyword>