Skip to main content

input-mocker is simple and easy-to-use tool for mocking of prompt functions.

Project description

# Input-Mocker

[![Build Status](https://travis-ci.org/ciotto/input-mocker.svg?branch=master)](https://travis-ci.org/ciotto/input-mocker)
[![codecov](https://codecov.io/gh/ciotto/input-mocker/branch/master/graph/badge.svg)](https://codecov.io/gh/ciotto/input-mocker)

-[![Code Health](https://landscape.io/github/ciotto/input-mocker/master/landscape.svg?style=flat)](https://landscape.io/github/ciotto/input-mocker/master)

[![PyPI version](https://badge.fury.io/py/input-mocker.svg)](https://badge.fury.io/py/input-mocker)

[![Py versions](https://img.shields.io/pypi/pyversions/input-mocker.svg)](https://pypi.python.org/pypi/input-mocker/)
[![License](https://img.shields.io/pypi/l/input-mocker.svg)](https://pypi.python.org/pypi/input-mocker/)

[![PyPI pyversions](https://img.shields.io/pypi/pyversions/input-mocker.svg)](https://pypi.python.org/pypi/input-mocker/)
[![PyPI status](https://img.shields.io/pypi/status/input-mocker.svg)](https://pypi.python.org/pypi/input-mocker/)

**input-mocker** is *simple* and *easy-to-use* tool for mocking of prompt functions.

You can mock call to a `sys.stdin.readline in order to programmatically send user input.


## Requirements

Python 2.7, 3.4, 3.5, 3.6


## Installation

Run `pip install https://github.com/ciotto/input-mocker/archive/master.zip`
Run `pip install input-mocker`

## Use

```
import unittest
import sys
import time
from .fake import _input
from input_mocker import InputMocker, TimeoutTestCaseMixin
from timeout_decorator import TimeoutError

class TestInputMocker(unittest.TestCase, TimeoutTestCaseMixin):
def test_assert_timeout(self):
self.assertTimeout(self._input)
self.assertTimeout(self._input, args=['Foo'])

def test_assert_not_timeout(self):
string = 'Foo Bar'
result = self.assertNotTimeout(string.split, args=[' '])
self.assertEqual(result, ['Foo', 'Bar'])
result = self.assertNotTimeout(unicode, args=['Foo'], kwargs={'encoding': 'utf-8'})
self.assertEqual(result, u'Foo')

def test_assert_timeout_exception(self):
string = 'Foo Bar'
with self.assertRaises(TimeoutError):
self.assertTimeout(self._input, raise_exception=True)

def test_sys_stdin_readline(self):
mocker = InputMocker()
with mocker.patch():
results = []
loops = 10
for i in range(0, loops):
result = self.assertNotTimeout(sys.stdin.readline)
results.append(result)

self.assertEqual(results, ['y', 'n'] * int(loops / 2))

self.assertTimeout(self._input)

mocker = InputMocker(answers=['foo', 'bar'])
with mocker.patch():
results = []
loops = 10
for i in range(0, loops):
result = self.assertNotTimeout(sys.stdin.readline)
results.append(result)

self.assertEqual(results, ['foo', 'bar'] * int(loops / 2))

self.assertTimeout(self._input)

mocker = InputMocker(random=True)
with mocker.patch():
results = []
loops = 10
for i in range(0, loops):
result = self.assertNotTimeout(sys.stdin.readline)
results.append(result)

self.assertNotEquals(results, ['y', 'n'] * int(loops / 2))

@InputMocker().patch()
def test_input_mocker(self):
with InputMocker().patch():
results = []
loops = 10
for i in range(0, loops):
result = self.assertNotTimeout(self._input)
results.append(result)

self.assertEqual(results, ['y', 'n'] * int(loops / 2))
```

## Contribute

```
$ git clone https://github.com/ciotto/input-mocker.git
$ make init
```

you can run test with command `make test`


---

## License

Released under [MIT License](LICENSE.txt).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

input-mocker-0.1.0.macosx-10.11-x86_64.tar.gz (4.3 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page