├── .gitignore ├── .travis.yml ├── LICENSE ├── MANIFEST.in ├── README.rst ├── pytest_mockito ├── __init__.py └── plugin.py ├── setup.py └── tests ├── __init__.py └── fixtures_test.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.egg-info/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - '2.7' 4 | - '3.4' 5 | - '3.5' 6 | - '3.6' 7 | install: 8 | - pip install pytest --upgrade 9 | - pip install . 10 | script: 11 | - py.test tests 12 | deploy: 13 | provider: pypi 14 | user: herrkaste 15 | password: 16 | secure: mjtqU413O5h4IYhB9iZVy5FqTGwqclvBrQXewQDps+uJJIuyhCVB7EYmWf+yBt22BF+iJtde/z9yJkuLm+wvsLRvg7YJRcNDbKlGJeOiNgcR6fiBUAKcTm3MTNXyKZhQ9YvTO2Hpk2Be+18867HIyC7K2IiUi+AYY/YwX7GCoQnXTRjQ4GGmAOnUW+bGFIQNjWz7j40UQMfofkW5br+j1OERD2OGK60VcNWb6hQPLB+KQRbQ7fxkf7ODkov3rixb5VaFQqhjxgfubGMSLSA9h3S21n8STGTxgJZnnyjmI1+4FbD+VtbZLTQivaqfkexqxt/EAdU5S6mITCSbajoXMn0qTW10vMCn6dSlhBrob/1JINi9Wu2c9tj6PDeQ0hO1MJKL00wNSFdyrkgef7qsgdByPunJjkjT9Ihfsi9GUSWz4OvmIYjfbejzwNqWuEgBa4iLDKTNYsOVJHIIgJX3Z93k/O11JlalZtwSOou9CNB8CC3u1anZbA2ZahUIJV7DfcaoDzXiC2V2Zm2gNOxUeaHFHJekSLlTErCQiFNr3uQL31J10EvX2AvuOPAwdSdrUDIkcTq52o0Jq47eRoDV/27bOiXxDLfAhbPTcLzSrneI5ITu9Q7YhP1fs7sYaWeNv2II4veQz+Ei0vWjeLVcvDue5JzVZPbdmIqh4zErIXs= 17 | on: 18 | tags: true 19 | distributions: sdist bdist_wheel 20 | repo: kaste/pytest-mockito 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2014 Herr Kaste (herr.kaste@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.rst 2 | include LICENSE 3 | include setup.py -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Convenience plugin on top of mockito. 2 | 3 | .. image:: https://travis-ci.org/kaste/pytest-mockito.svg?branch=master 4 | :target: https://travis-ci.org/kaste/pytest-mockito 5 | 6 | Install 7 | ======= 8 | 9 | ``pip install pytest-mockito`` 10 | 11 | After that the plugin is enabled by default. 12 | 13 | 14 | Fixtures 15 | ======== 16 | 17 | The plugin provides fixtures for the main entrypoints of mockito which guarantee that you `unstub()` on teardown. Usage is *very* simple and straightforward:: 18 | 19 | def test_foo(when): 20 | when(os.path).exists('/foo').thenReturn(False) 21 | assert os.path.exists('/foo') # sic! 22 | # will still unstub/unpatch bc pytest will run the teardown 23 | 24 | For convenience `verifyStubbedInvocationsAreUsed` is called just before `unstub`. This should warn you when you setup stubs that you actually don't use. 25 | 26 | You can also use a marker. `usefixtures `_ here will ensure an `unstub` at the end of each test, but does not actually inject the fixture:: 27 | 28 | import pytest 29 | 30 | @pytest.mark.usefixtures('unstub') 31 | class TestDog: 32 | def test(self): 33 | ... 34 | 35 | To mark *all* test cases at the module level:: 36 | 37 | pytestmark = pytest.mark.usefixtures('unstub') 38 | 39 | 40 | All of the following fixtures just export the equivalent mockito function but `unstub()` on teardown. The exception here is `expect` which also calls `verifyNoUnwantedInteractions()`:: 41 | 42 | when 43 | when2 44 | expect 45 | patch 46 | unstub 47 | spy2 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /pytest_mockito/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaste/pytest-mockito/f20cae8f592cf83682f6027c7aafafc829c13785/pytest_mockito/__init__.py -------------------------------------------------------------------------------- /pytest_mockito/plugin.py: -------------------------------------------------------------------------------- 1 | 2 | import pytest 3 | 4 | 5 | @pytest.fixture 6 | def unstub_(): 7 | from mockito import unstub 8 | yield unstub 9 | unstub() 10 | 11 | 12 | @pytest.fixture 13 | def unstub(unstub_): 14 | from mockito import verifyStubbedInvocationsAreUsed 15 | yield unstub_ 16 | 17 | verifyStubbedInvocationsAreUsed() 18 | 19 | 20 | @pytest.fixture 21 | def when(unstub): 22 | from mockito import when 23 | yield when 24 | 25 | 26 | @pytest.fixture 27 | def when2(unstub): 28 | from mockito import when2 29 | yield when2 30 | 31 | 32 | @pytest.fixture 33 | def expect(unstub): 34 | from mockito import expect, verifyNoUnwantedInteractions 35 | yield expect 36 | verifyNoUnwantedInteractions() 37 | 38 | 39 | @pytest.fixture 40 | def patch(unstub): 41 | from mockito import patch 42 | yield patch 43 | 44 | 45 | @pytest.fixture 46 | def spy2(unstub): 47 | from mockito import spy2 48 | yield spy2 49 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | setup( 4 | name="pytest-mockito", 5 | version='0.0.4', 6 | description='Base fixtures for mockito', 7 | long_description=open('README.rst').read(), 8 | license='MIT', 9 | author='herr kaste', 10 | author_email='herr.kaste@gmail.com', 11 | url='https://github.com/kaste/pytest-mockito', 12 | platforms=['linux', 'osx', 'win32'], 13 | packages=['pytest_mockito'], 14 | entry_points={'pytest11': ['mockito = pytest_mockito.plugin'], }, 15 | zip_safe=False, 16 | install_requires=['pytest>=3', 'mockito>=1.0.6'], 17 | classifiers=[ 18 | 'Development Status :: 4 - Beta', 19 | 'Intended Audience :: Developers', 20 | 'License :: OSI Approved :: MIT License', 21 | 'Operating System :: POSIX', 22 | 'Operating System :: Microsoft :: Windows', 23 | 'Operating System :: MacOS :: MacOS X', 24 | 'Topic :: Software Development :: Testing', 25 | 'Topic :: Software Development :: Quality Assurance', 26 | 'Topic :: Utilities', 27 | 'Programming Language :: Python', 28 | ], 29 | ) 30 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaste/pytest-mockito/f20cae8f592cf83682f6027c7aafafc829c13785/tests/__init__.py -------------------------------------------------------------------------------- /tests/fixtures_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | import os 4 | 5 | 6 | class TestEnsureUnstubForFailingTests: 7 | 8 | @pytest.mark.xfail() 9 | def testFail(self, when): 10 | when(os.path).exists('/Bar').thenReturn(True) 11 | assert not os.path.exists('/Bar') # sic! 12 | 13 | def testCheckIfCleaned(self): 14 | assert not os.path.exists('/Bar') 15 | 16 | def testSanityCheck(self, when): 17 | when(os.path).exists('/Foo').thenReturn(True) 18 | assert os.path.exists('/Foo') # sanity check 19 | 20 | 21 | class TestEnsureUnstubWhenStubsAreUnused: 22 | 23 | @pytest.mark.xfail() 24 | def testFails(self, when): 25 | when(os.path).exists('/Bar').thenReturn(True) 26 | 27 | def testPass(self, when): 28 | when(os.path).exists('/Bar').thenReturn(True) 29 | assert os.path.exists('/Bar') 30 | 31 | --------------------------------------------------------------------------------