author | Lennard de Rijk <ljvderijk@gmail.com> |
Sat, 04 Jul 2009 14:19:39 +0200 | |
changeset 2528 | 9de8348890d8 |
parent 1000 | 9af147fc1f1c |
permissions | -rw-r--r-- |
1000
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1 |
Mox is an open source mock object framework for Python, inspired by |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
2 |
the Java library EasyMock. |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
3 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
4 |
To install: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
5 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
6 |
$ python setup.py install |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
7 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
8 |
To run Mox's internal tests: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
9 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
10 |
$ python mox_test.py |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
11 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
12 |
Basic usage: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
13 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
14 |
import unittest |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
15 |
import mox |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
16 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
17 |
class PersonTest(mox.MoxTestBase): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
18 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
19 |
def testUsingMox(self): |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
20 |
# Create a mock Person |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
21 |
mock_person = self.mox.CreateMock(Person) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
22 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
23 |
test_person = ... |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
24 |
test_primary_key = ... |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
25 |
unknown_person = ... |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
26 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
27 |
# Expect InsertPerson to be called with test_person; return |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
28 |
# test_primary_key at that point |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
29 |
mock_person.InsertPerson(test_person).AndReturn(test_primary_key) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
30 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
31 |
# Raise an exception when this is called |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
32 |
mock_person.DeletePerson(unknown_person).AndRaise(UnknownPersonError()) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
33 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
34 |
# Switch from record mode to replay mode |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
35 |
self.mox.ReplayAll() |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
36 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
37 |
# Run the test |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
38 |
ret_pk = mock_person.InsertPerson(test_person) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
39 |
self.assertEquals(test_primary_key, ret_pk) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
40 |
self.assertRaises(UnknownPersonError, mock_person, unknown_person) |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
41 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
42 |
For more documentation, see: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
43 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
44 |
http://code.google.com/p/pymox/wiki/MoxDocumentation |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
45 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
46 |
For more information, see: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
47 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
48 |
http://code.google.com/p/pymox/ |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
49 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
50 |
Our user and developer discussion group is: |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
51 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
52 |
http://groups.google.com/group/mox-discuss |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
53 |
|
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
54 |
Mox is Copyright 2008 Google Inc, and licensed under the Apache |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
55 |
License, Version 2.0; see the file COPYING for details. If you would |
9af147fc1f1c
Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
56 |
like to help us improve Mox, join the group. |