How can you unit test nested functions? Or do you have to pull them out to
unit test them, which basically means I will never use nested functions.
Also, same thing with private member functions protected by __. Seems like
there is a conflict there between using these features and unit testing.
Andy 5 3406
Andy wrote: How can you unit test nested functions?
I can't think of a good way. When I write a nested function it's because
the function uses variables from the scope of the function in which it's
embedded, which means it makes little sense to test it independent of the
larger function.
My tests in that case are only of the enclosing function.
Or do you have to pull them out to unit test them, which basically means I will never use nested functions.
You don't test every line in a function by itself, right? Nor
every loop in a function. It should be possible to test the outer
function enough that the implementation detail - of using an inner
function - doesn't make much difference.
Also, same thing with private member functions protected by __. Seems like there is a conflict there between using these features and unit testing.
In that case the spec defined that the real function name is of
the form "_<CLASSNAME>__ <METHODNAME>" . For example class Spam:
.... def __sing(self):
.... print "I don't see any Vikings."
.... spam = Spam() spam._Spam__sin g()
I don't see any Vikings.
I've found though that the double-leading-underscore is overkill.
Using a single underscore is enough of a hint that the given
method shouldn't be called directly.
Then again, I don't write enough deep hierarchies where I need
to worry about a subclass implementation using the same private
name as a superclass.
Andrew da***@dalkescie ntific.com
[Andy] How can you unit test nested functions? Or do you have to pull them out to unit test them, which basically means I will never use nested functions.
Several commons use cases (closures and factory functions) ultimately
expose the inner function through the return value. If that is the
case, the answer is simple, call the enclosing function and then run
the test on the result.
If the inner function never gets exposed, an argument could be made
that you don't want to write a test for it -- that the inner function
is just an implementation detail. This is black box testing and
consistent with a test driven development approach.
For whitebox testing, you could make an inner function visible by
binding it to the enclosing function's attribute namespace.
def f(x):
def g(y):
. . .
f.g = g # make g visible as an attribute of f
. . .
Raymond
Raymond Hettinger wrote: [Andy]How can you unit test nested functions?
For whitebox testing, you could make an inner function visible by binding it to the enclosing function's attribute namespace.
def f(x): def g(y): . . . f.g = g # make g visible as an attribute of f . . .
Note that when using this technique, f.g will not be bound until after
you call the function: def f(x):
.... def g(y):
.... pass
.... f.g = g
.... f
<function f at 0xb7df3df4> f.g
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'function' object has no attribute 'g' f(1) f.g
<function g at 0xb7df3e2c>
--
Benji York
> > [Andy] How can you unit test nested functions?
[Raymond Hettinger] For whitebox testing, you could make an inner function visible by binding it to the enclosing function's attribute namespace.
def f(x): def g(y): . . . f.g = g # make g visible as an attribute of f . . .
[Benji York] Note that when using this technique, f.g will not be bound until after you call the function:
That is a feature, not a bug. The inner function isn't even created
until the outer function is run.
def f(x):
z = 30
def g(y):
return 10
return 20
from dis import dis dis(f)
2 0 LOAD_CONST 1 (30)
3 STORE_FAST 1 (z)
3 6 LOAD_CONST 2 (<code object g at 00A33660,
file "<pyshell#3 7>", line 3>)
9 MAKE_FUNCTION 0
12 STORE_FAST 2 (g)
5 15 LOAD_CONST 3 (20)
18 RETURN_VALUE
The MAKE_FUNCTION step is where g comes into existence. That is a
run-time operation, not compile time.
If you are willing to be tricky, it is possible to write an accessor
function that goes into f.func_code.co_ consts, finds the code object
whose co_name attribute is 'g', builds a wrapper function using
types.FunctionT ype, and returns the result for unittesting. But that
is not for the faint of heart ;-)
Raymond
Raymond Hettinger wrote: [Benji York]
Note that when using this technique, f.g will not be bound until after you call the function:
That is a feature, not a bug. The inner function isn't even created until the outer function is run.
I'm fully aware of that. I just didn't want the OP get bit by it.
--
Benji York This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: block111 |
last post by:
Hello,
code like this:
int f1(int x){
int f2(int y){
return y*y;
}
if(x > 0)
return f2(x);
|
by: sylcheung |
last post by:
Is it possible to use python to unit test C++ code? If yes, is there
any example available?
Thank you.
|
by: yashgt |
last post by:
Hi,
I have installed Visual Studio 2005 Team Edition for Developers, on my
Windows XP PC.
The exact VS version is 8.0.50727.42.
I created a simple Windows application in VB .NET and add a button and
its handler. I then tried to add a Unit test case for the handler,
using Test>New Test>Unit Test Wizard. The handler for the button is not
seen the the list of functions available to be unit tested. The Form1
class itself is not listed.
On a...
|
by: micklee74 |
last post by:
hi
just curious , if i have a code like this?
def a():
def b():
print "b"
def c():
print "c"
how can i call c() ??
|
by: VvanN |
last post by:
hi, fellows
I'd like to intruduce a new unit test framework for C++
freely available at:
http://unit--.sourceforge.net/
It does not need bothering test registration, here is an example
// --- begin code ---
| |
by: bearophileHUGS |
last post by:
Recently I have posted this same question on it.comp.lang.python, maybe
there aren't solutions, but I'd like to know what you think.
Can doctests be added to nested functions too? (This can be useful to
me, I use nested function when I don't have attributes that I have to
remember, but I want to split the logic in some subparts anyway).
Example:
def foo():
"""
|
by: shuisheng |
last post by:
Dear All,
I was told that unit test is a powerful tool for progamming. If I am
writing a GUI code, is it possible to still using unit test?
I have a little experience in using unittest++. But I can not work out
a way to use it to test GUI code.
Thanks a lot!
|
by: rich_sposato |
last post by:
I released version 2.0 of C++ Unit Test Library. You can download
it from SourceForget.Net at http://sourceforge.net/projects/cppunittest/
..
I wrote this unit test library because other unit test frameworks
always lacked features I considered important. Some only provided
output in certain formats, and I had no easy way to choose my own
format. Most provided no protection against exceptions. Only a few
were useful for unit-testing...
|
by: Vyacheslav Maslov |
last post by:
Hi all!
I have many many many python unit test, which are used for testing some
remote web service.
The most important issue here is logging of test execution process and
result. I strongly need following:
1. start/end timestamp for each test case (most important)
2. immediate report about exceptions (stacktrace)
3. it will be nice to use logging module for output
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |