473,394 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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(self):
"""Value and multiplier must be readonly"""
try:
self.combat.value = 1
self.fail("Value 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 2186
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(self):
"""Value and multiplier must be readonly"""
try:
self.combat.value = 1
self.fail("Value 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(self, instance, attribute, value=1):
"""Value and multiplier must be readonly"""
try:
setattr(instance, attribute, value)
self.fail("Value is not read only")
except AttributeError:
pass
Then the testing becomes one line:

self.test_readonly(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(self):
"""Value and multiplier must be readonly"""
try:
self.combat.value = 1
self.fail("Value 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.assertRaises(AttributeError, lambda: self.combat.value = 1)
Jul 18 '05 #3
Roy Smith wrote:
You want something like

self.assertRaises(AttributeError, lambda: self.combat.value = 1)


Or, combining the two responses and avoiding the lambda:

self.assertRaises(AttributeError, 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.assertRaises(AttributeError, lambda: self.combat.value = 1)


Or, combining the two responses and avoiding the lambda:

self.assertRaises(AttributeError, 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.assertRaises(AttributeError, lambda: self.combat.value = 1)


Or, combining the two responses and avoiding the lambda:

self.assertRaises(AttributeError, 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.assertRaises(AttributeError, lambda: self.combat.value = 1)


Or, combining the two responses and avoiding the lambda:

self.assertRaises(AttributeError, 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.assertRaises(AttributeError, setattr(self.combat, '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**********@invalid.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.assertRaises(AttributeError, setattr(self.combat, '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.invalid> 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
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...
1
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...
3
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 ...
13
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...
1
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...
6
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"/> .... ...
1
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...
1
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.