Questions tagged [mox]

Mox is a mock object framework for Python.

Mox is based on EasyMock, a Java mock object framework.

Mox will make mock objects for you, so you don't have to create your own! It mocks the public/protected interfaces of Python objects. You set up your mock objects expected behavior using a domain specific language (DSL), which makes it easy to use, understand, and refactor!

38 questions
31
votes
8 answers

Mocking open(file_name) in unit tests

I have a source code that opens a csv file and sets up a header to value association. The source code is given below: def ParseCsvFile(source): """Parse the csv file. Args: source: file to be parsed Returns: the list of dictionary…
Kartik
  • 2,541
  • 2
  • 37
  • 59
12
votes
1 answer

Python easy way to read all import statements from py module

I am trying to create a helper function to read a file and mock out all imports for a unit test. I have to read the file vs import since i dont have those things on python path. Example code: #module.py import com.stackoverflow.question from…
Nix
  • 57,072
  • 29
  • 149
  • 198
10
votes
1 answer

Testing multiple API calls in a function chain with Mox

I'm trying to test that I'm transforming the data coming back from a third party api correctly. I'm hitting some trouble with Mox because I need to hit two separate endpoints during the data transformation. Let me explain more clearly by posting…
Bitwise
  • 8,021
  • 22
  • 70
  • 161
8
votes
2 answers

Python SQLAlchemy - Mocking a model attribute's "desc" method

In my application, there is a class for each model that holds commonly used queries (I guess it's somewhat of a "Repository" in DDD language). Each of these classes is passed the SQLAlchemy session object to create queries with upon construction.…
rr.
  • 6,484
  • 9
  • 40
  • 48
7
votes
1 answer

AttributeError: __exit__ when I try to mock out build in functions

I'm currently trying to mock out the open() built in method in Python for a test. However, I always end up getting a crash and this resulting message: File "/opt/home/venv/lib/python2.7/site-packages/nose-1.3.0-py2.7.egg/nose/result.py", line…
user2253332
  • 797
  • 3
  • 12
  • 21
5
votes
2 answers

How do I mock a class property with mox?

I have a class: class MyClass(object): @property def myproperty(self): return 'hello' Using mox and py.test, how do I mock out myproperty? I've tried: mock.StubOutWithMock(myclass, 'myproperty') myclass.myproperty =…
Harley Holcombe
  • 175,848
  • 15
  • 70
  • 63
4
votes
1 answer

Mocks or Stubs?

I have a method that calls two other methods in it. def main_method(self, query): result = self.method_one(query) count = self.method_two(result) return count def method_one(self, query): #Do some stuff based on results. #This method hits…
mohi666
  • 6,842
  • 9
  • 45
  • 51
4
votes
2 answers

How do I use Mox to mock a module function and allow it to be called in almost any way

I have a function A that call another function B several times. I want to mock B in such a way that any number of calls that have the correct number of arguments, regardless of value, will return a fixed vale and be treated as correct. If or how…
BCS
  • 75,627
  • 68
  • 187
  • 294
3
votes
1 answer

How to mock out a function that's returned from getattr?

I have a class that does something like: class MyClass(object): def __init__(self, delegate_to): self._delegate_to = delegate_to def __getattr__(self, item): return getattr(self._delegate_to, item) But when I try to do…
Noel Yap
  • 18,822
  • 21
  • 92
  • 144
2
votes
1 answer

How to mock Pythonic way?

Mox mocking library allows you to be either specific or agnostic about the class you are mocking. mock = mox.CreateMock(Foo) or mock = mox.CreateMockAnything() Mox documentation suggests to use the first way (basically check the type of the mock)…
Alex
  • 93
  • 1
  • 6
2
votes
1 answer

Mocking objects in Python

I'm new to Unit testing and mocking objects in Python. I have a function that I need to create a unit test for. def BuildBall(self, material): """Create a Ball from material.""" result = {} for b in xrange(material.ball_size()): ball =…
mohi666
  • 6,842
  • 9
  • 45
  • 51
2
votes
2 answers

Use mox to mock a method called by__init__

I'd like to stub out a single method in a class that is called by the init method. class MyClass(object): def __init__(self): # Some initializer code here ... self.method_with_side_effects() def method_with_side_effects(self): …
Lorin Hochstein
  • 57,372
  • 31
  • 105
  • 141
2
votes
1 answer

Mock a function which is called when a module is imported

I want to test a module A which uses decorators with arguments. The arguments get evaluated when the module A is loaded. For some of the decorator args, I set the value by calling a function foo in module B. # A.py import B @deco(arg1=B.foo()) def…
CppNoob
  • 2,322
  • 1
  • 24
  • 35
2
votes
3 answers

Mocking urllib2.urlopen and lxml.etree.parse using pymox

I'm trying to test some python code that uses urllib2 and lxml. I've seen several blog posts and stack overflow posts where people want to test exceptions being thrown, with urllib2. I haven't seen examples testing successful calls. Am I going down…
jmkacz
  • 63
  • 2
  • 7
2
votes
1 answer

How to mock method call without signature check?

I use mox to mock an object. I need to record method call but the method receives a lot of arguments I am not intetested in, and some of this arguments are not primitive types but instances of classes created somewhere else. I want to avoid the…
peroksid
  • 890
  • 6
  • 17
1
2 3