Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make py.test tests accept interactive input?

Tags:

python

pytest

I'm using py.test for a somewhat unconventional application. Basically, I want to have user interaction in a test via print() and input() (this is Python 3.5). The ultimate goal is to have semi-automated testing for hardware and multi-layered software which cannot be automatically tested even in principle. Some test cases will ask the testing technician to do something (to be confirmed by enter or press any key or similar on the console) or ask them to take a simple measurement or visually confirm something (to be entered on the console).

Example of what I (naively) want to do:

def test_thingie():
    thingie_init('red')
    print('Testing the thingie.')
    # Ask the testing technician to enter info, or confirm that he has set things up physically
    x = int(input('Technician: How many *RED* widgets are on the thingie? Enter integer:')) 
    assert x == the_correct_number

This works with when the test file is called with pytest -s to prevent stdin and stdout capturing, but the documented means (with capsys.disabled()) in the py.test documentation don't work since they only affect stdout and stderr.

What's a good way to make this work using code in the py.test module, no command line options, and ideally per-test?

The platform, for what it's worth, is Windows and I'd prefer not to have this clobber or get clobbered by the wrapping of stdin/out/whatever that results from nested shells, uncommon shells, etc.

like image 361
ikrase Avatar asked Dec 17 '16 00:12

ikrase


People also ask

Can pytest be used for functional testing?

Pytest: Best among all Python testing frameworks, Pytest is a Python testing framework which provides a single solution for Unit, Functional and Acceptance testing. It is popular than the other available frameworks because of its attractive features.

How does pytest know what to run?

Pytest automatically identifies those files as test files. We can make pytest run other filenames by explicitly mentioning them. Pytest requires the test function names to start with test. Function names which are not of format test* are not considered as test functions by pytest.

Is pytest third party?

A number of third-party testing frameworks attempt to address some of the issues with unittest , and pytest has proven to be one of the most popular.


1 Answers

no command line options

Use pytest.ini option or env variable to avoid using command line option every time.

ideally per-test?

Use function scoped fixture to take user input. Example code:

# contents of conftest.py
import pytest
@pytest.fixture(scope='function')
def take_input(request):
    val = input(request.param)
    return val



#Content of test_input.py
import pytest

@pytest.mark.parametrize('prompt',('Enter value here:'), indirect=True)
def test_input(take_input):
    assert take_input == "expected string"
like image 80
SilentGuy Avatar answered Sep 18 '22 14:09

SilentGuy