473,839 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unittest - testing properties (read-only attributes)

I have a class with a read-only attribute, and I want to add a unit
test to ensure that it really *is* read-only. I can do this as

def test_readonly(s elf):
"""Value and multiplier must be readonly"""
try:
self.combat.val ue = 1
self.fail("Valu e is not read only")
except AttributeError:
pass

That works, but it seems a bit clumsy. Is there a better way?

Thanks,
Paul.
--
XML with elementtree is what makes me never have think about XML
again. -- Istvan Albert
Jul 18 '05 #1
9 2208
Paul Moore wrote:
I have a class with a read-only attribute, and I want to add a unit
test to ensure that it really *is* read-only. I can do this as

def test_readonly(s elf):
"""Value and multiplier must be readonly"""
try:
self.combat.val ue = 1
self.fail("Valu e is not read only")
except AttributeError:
pass

That works, but it seems a bit clumsy. Is there a better way?

By using setattr, you could refactor the above code into a function. Looks
like this (untested):

def test_readonly(s elf, instance, attribute, value=1):
"""Value and multiplier must be readonly"""
try:
setattr(instanc e, attribute, value)
self.fail("Valu e is not read only")
except AttributeError:
pass
Then the testing becomes one line:

self.test_reado nly(self.combat , "value")
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
In article <uu***********@ yahoo.co.uk>, Paul Moore <pf******@yahoo .co.uk>
wrote:
I have a class with a read-only attribute, and I want to add a unit
test to ensure that it really *is* read-only. I can do this as

def test_readonly(s elf):
"""Value and multiplier must be readonly"""
try:
self.combat.val ue = 1
self.fail("Valu e is not read only")
except AttributeError:
pass

That works, but it seems a bit clumsy. Is there a better way?

Thanks,
Paul.


You want something like

self.assertRais es(AttributeErr or, lambda: self.combat.val ue = 1)
Jul 18 '05 #3
Roy Smith wrote:
You want something like

self.assertRais es(AttributeErr or, lambda: self.combat.val ue = 1)


Or, combining the two responses and avoiding the lambda:

self.assertRais es(AttributeErr or, setattr, self.combat, 'value', 1)

Hmm... this might be a case where the lambda form is actually the
more readable one...

-Peter
Jul 18 '05 #4
Peter Hansen <pe***@engcorp. com> writes:
You want something like
self.assertRais es(AttributeErr or, lambda: self.combat.val ue = 1)


Or, combining the two responses and avoiding the lambda:

self.assertRais es(AttributeErr or, setattr, self.combat, 'value', 1)

Hmm... this might be a case where the lambda form is actually the
more readable one...


Yes, assignment expressions could make code more readable, if Python
supported them.
Jul 18 '05 #5
Peter Hansen <pe***@engcorp. com> writes:
Roy Smith wrote:
You want something like
self.assertRais es(AttributeErr or, lambda: self.combat.val ue = 1)


Or, combining the two responses and avoiding the lambda:

self.assertRais es(AttributeErr or, setattr, self.combat, 'value', 1)

Hmm... this might be a case where the lambda form is actually the
more readable one...


Thanks, I hadn't thought of setattr. I was bitten by the "assignment
is a statement, so can't be used in a lambda" issue, as well :-)

Paul.
--
It was a machine, and as such only understood one thing. Being clobbered
with big hammers was something it could relate to. -- Tom Holt
Jul 18 '05 #6
Paul Rubin wrote:
Peter Hansen <pe***@engcorp. com> writes:
> You want something like
> self.assertRais es(AttributeErr or, lambda: self.combat.val ue = 1)


Or, combining the two responses and avoiding the lambda:

self.assertRais es(AttributeErr or, setattr, self.combat, 'value', 1)

Hmm... this might be a case where the lambda form is actually the
more readable one...


Yes, assignment expressions could make code more readable, if Python
supported them.


An assignment expression, if such a thing existed wouldn't help here.

The point being that the expression must be evaluated inside the exception
handler in assertRaises, so you either need to delay the evaluation with a
lambda, or by passing the function and arguments in separately. If you had
an assignment expression it would be roughly equivalent to:

self.assertRais es(AttributeErr or, setattr(self.co mbat, 'value', 1))

which will throw the AttributeError instead of passing the test.

Jul 18 '05 #7
Duncan Booth wrote:
An assignment expression, if such a thing existed wouldn't help here.


Although of course it would help if still inside a lambda.
Jul 18 '05 #8
Duncan Booth <du**********@i nvalid.invalid> writes:
An assignment expression, if such a thing existed wouldn't help here.

The point being that the expression must be evaluated inside the exception
handler in assertRaises, so you either need to delay the evaluation with a
lambda, or by passing the function and arguments in separately. If you had
an assignment expression it would be roughly equivalent to:

self.assertRais es(AttributeErr or, setattr(self.co mbat, 'value', 1))

which will throw the AttributeError instead of passing the test.


Yes. The example I quoted used an assignment expression inside a
lambda. The person who posted it, and the person who followed it up
with the setattr alternative, both didn't notice that the assignment
expression wasn't valid Python. However, my post came out sounding
grumpier than I intended ;).
Jul 18 '05 #9
Paul Rubin <http://ph****@NOSPAM.i nvalid> wrote:
The example I quoted used an assignment expression inside a
lambda. The person who posted it,
That was me.
and the person who followed it up
with the setattr alternative, both didn't notice that the assignment
expression wasn't valid Python.
Ugh. No, I hadn't noticed it. Thanks for pointing it out.

I don't use lambdas much. In fact, the only time I ever do use them is for
an assertRaises unit test. I've always thought that "assignment is a
statement not an expression" was one of Python's warts, and this is just
another example of why it is. Of course, "lambda isn't just a def body" is
a wart too :-)
However, my post came out sounding
grumpier than I intended ;).


Actually, I hadn't noticed that either. :-)
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2592
by: Paul Moore | last post by:
One of the things I really dislike about Unittest (compared, say, to a number of adhoc testing tricks I've used in the past, and to Perl's "standard" testing framework) is that the testcase-as-a-class model tends to imply a relatively high granularity in testing. A good example of this comes from "Dive Into Python" (http://diveintopython.org) section 7.3. Here, the author has written a module which converts numbers to Roman numerals. The...
1
1411
by: kobayashi | last post by:
Hi all python users, A question about readonly attributes once again, but even reading the threads about "properties" and "descriptors", I can't find exactly what I want. Let a developper to write the class class A(object): def __init__(self):
3
9930
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ========================= <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0"
13
3147
by: Patrick * | last post by:
Reading a book on .ASP .NET I am getting a bit mixed up as to the difference between "property" and "attribute" as the terms don't seem to be used consistently in the book I am reading. Can anyone give two good definitions? Both with respect to controls.
1
1346
by: farseer | last post by:
Hi, i have an object containing several properties thati would like to serialize to an xml. i'd like certain properties serialized as attributes and others as elements. What is the best way to do this? can i simply preface the property with XmlAttribute? If wanted them to be added with a specific namespace...how do i do this? thank you
6
1639
by: Abdullah Kauchali | last post by:
How does one define additional attributes for elements in an XSD? So, I'd like something like this: .... <xs:element name="ClaimNumber" type="xs:string" mycustomproperty="xs:string"/> .... Ideally, I'd like Visual Studio (2005) to allow the entry of these
1
1103
by: Tom | last post by:
Hey, I am trying to create some new controls (I will parse the designer for controls and convert to a game xml format for GUI), but i need to disable (I.e. readonly and possibly remove from the control so that the user cannot see them in the propertygrid), so that they can only change properties that will actually convert to the game format. Cheers
1
2848
by: mtembene | last post by:
I have a windows form "BaseForm" that contains a DataGridView that is not bound to any datasources and a button. Both of these controls have a modifier of "Protected Internal" and none of the controls are locked on this form. I have another windows form call "ChildForm" that inherits "BaseForm". When I open"BaseForm: in VS2005 I can see both the DataGridView and the button and I can view all of the properties of these controls. ...
0
9855
marktang
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...
0
10908
Oralloy
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...
0
10587
jinu1996
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...
1
10649
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7018
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();...
0
5682
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...
0
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4487
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.