Issue 37 in pymox: Optional arguments

53 views
Skip to first unread message

codesite...@google.com

unread,
Aug 16, 2011, 5:13:00 AM8/16/11
to mox-d...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 37 by stefano....@gmail.com: Optional arguments
http://code.google.com/p/pymox/issues/detail?id=37

Related to issue 12, I want to stub out a function with optional arguments.
I can't know in advance whether the optional arguments will be provided or
not.
IgnoreArg() is no help with missing arguments.

def bar(one, two=None):
pass

The test subject may call
bar(1)
or
bar(1, two=None)
or
bar(1, None)
or
bar(1, 42)

I'd like to be able to do
bar(1, two=IgnoreArg())
and have it match all of the above.

codesite...@google.com

unread,
Aug 16, 2011, 11:53:35 AM8/16/11
to mox-d...@googlegroups.com
Updates:
Status: WontFix

Comment #1 on issue 37 by steve.mi...@gmail.com: Optional arguments
http://code.google.com/p/pymox/issues/detail?id=37

Mox is intended to be used for deterministic tests.

Why can't you know in advance what your code is going to do?

codesite...@google.com

unread,
Aug 16, 2011, 12:18:59 PM8/16/11
to mox-d...@googlegroups.com

Comment #2 on issue 37 by stefano....@gmail.com: Optional arguments
http://code.google.com/p/pymox/issues/detail?id=37

OK, I only really need the first two examples above.

I have to stub out a function in another library for a test, and there are
multiple versions of this library in production. The new version passes
two=None, the other doesn't.

codesite...@google.com

unread,
Sep 22, 2015, 11:13:34 AM9/22/15
to mox-d...@googlegroups.com

Comment #3 on issue 37 by k...@google.com: Optional arguments
https://code.google.com/p/pymox/issues/detail?id=37

> Why can't you know in advance what your code is going to do?

Because Python, that's why!:D

The code under test may start off calling the mocked API without a given
arg – relying on the default value defined in that API, and then change to
pass the arg explicitly, with the same value as the arg's default, or vice
versa. In the real system it wouldn't change the behavior, but it breaks a
Mox test – creating negative value from the unnecessarily brittle test.

The point here is Python-the-language has a feature "default arg value",
meaning that passing the arg explicitly with that value or skipping it, has
the same effect at runtime. Mox does not allow to specify that semantics in
a mocked call.

A practical example. I am testing an API that makes RPC calls underneath,
and I want to mock and verify that it makes the right RPC calls. The RPC
stubs that I am mocking out, take an optional argument (default value None)
that I don't care about in the unit tests. I have a unit test that verifies
a retry after an RPC timeout. It so happens that the first call passes the
optional arg that I don't care about, and the retried calls don't. Yes it
is formally deterministic in the sense that I can mock these out, but at
that point I'd be testing the implementation not the interface, in a
brittle way – if the implementation changes to always pass the arg
explicitly (as in fact it just did), my tests break, with no benefit to me
as the test owner, only meaningless maintenance to adjust the test
expectations that have nothing to do with the real system behavior, but are
only an artifact of how Mox works.

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings
Reply all
Reply to author
Forward
0 new messages