Skip to content

Commit 05a641b

Browse files
committed
Initial commit of minimocktest to repo.
1 parent 0ab7461 commit 05a641b

File tree

5 files changed

+704
-0
lines changed

5 files changed

+704
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2011 Hatem Nassrat
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
MiniMockTest
2+
============
3+
4+
Documentation
5+
-------------
6+
7+
This module provides a class which extends unittest.TestCase which can
8+
be used to easily mock objects in a test case. This module can be used
9+
in conjunction with other test case classes by means of multiple
10+
inheritance http://docs.python.org/tutorial/classes.html#multiple-inheritance
11+
12+
An example of using Django's test case with this module's test case is
13+
simply as follows::
14+
15+
from minimocktest import MockTestCase
16+
from django.test import TestCase
17+
from django.test.client import Client
18+
19+
class DjangoTestCase(TestCase, MockTestCase):
20+
'''
21+
A TestCase class that combines minimocktest and django.test.TestCase
22+
'''
23+
24+
def setUp(self):
25+
TestCase.setUp(self)
26+
MockTestCase.setUp(self)
27+
# optional: shortcut client handle for quick testing
28+
self.client = Client()
29+
30+
def tearDown(self):
31+
MockTestCase.tearDown(self)
32+
TestCase.tearDown(self)
33+
34+
COPYING
35+
-------
36+
37+
This module is provided under the terms of the MIT Licence. The code in
38+
this module relies on the "minimock" library which is included as part of
39+
the package. The copyright notice for "minimock" is included at the
40+
beginning of its source code file.
41+
42+
For more information, see the file LICENCSE or
43+
http://www.opensource.org/licenses/mit-license.php

minimocktest/__init__.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# (c) 2011 Hatem Nassrat
2+
# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
3+
'''
4+
MiniMockTest module, holds MockTestCase class for easy to use mocking in testcases
5+
'''
6+
7+
import __builtin__
8+
import inspect
9+
from unittest import TestCase
10+
import minimock
11+
12+
class MockTestCase(TestCase):
13+
'''
14+
A TestCase class that integrates minimock functionailty
15+
`self.tt` minimock tracker object
16+
`self.mock` calls minimock.mock using tracker=`self.tt`
17+
`self.assertSameTrace` calls minimock.assert_same_trace with `self.tt`
18+
'''
19+
20+
Mock = minimock.Mock
21+
22+
def setUp(self):
23+
TestCase.setUp(self)
24+
self.tt = minimock.TraceTracker()
25+
26+
def tearDown(self):
27+
self.mockRestore()
28+
TestCase.tearDown(self)
29+
30+
def mock(self, *args, **kwargs):
31+
if len(args) <= 1 and 'nsdicts' not in kwargs:
32+
# custom nsdicts not used, inspect caller's stack
33+
stack = inspect.stack()
34+
try:
35+
# stack[1][0] is the frame object of the caller to this function
36+
globals_ = stack[1][0].f_globals
37+
locals_ = stack[1][0].f_locals
38+
nsdicts = (locals_, globals_, __builtin__.__dict__)
39+
finally:
40+
del(stack)
41+
kwargs['nsdicts'] = nsdicts
42+
if 'tracker' not in kwargs:
43+
kwargs['tracker'] = self.tt
44+
return minimock.mock(*args, **kwargs)
45+
46+
def mockRestore(self):
47+
minimock.restore()
48+
49+
def assertSameTrace(self, want):
50+
minimock.assert_same_trace(self.tt, want)

0 commit comments

Comments
 (0)