Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:58:10+00:00 2026-06-10T04:58:10+00:00

One of my attributes is a property where the setter calls a validation function

  • 0

One of my attributes is a property where the setter calls a validation function that raises an exception if the new value is invalid:

pos.offset = 0
# @offset.setter calls validate(offset=0)
# PositionError: Offset may not be 0.

I’m trying to add a test to ensure that this fails. However, I can’t figure out how to get assertRaises to work with an assignment.

The normal syntax of assertRaises requires a method, not an attribute/property:

self.assertRaises(PositionError, pos.offset, 0)
# TypeError: 'int' object is not callable

The other forms I’ve tried are invalid Python:

self.assertRaises(PositionError, pos.offset = 0)
# SyntaxError: Keyword can't be an expression
self.assertRaises(PositionError, lambda: pos.offset = 0)
# SyntaxError: lambda cannot contain assignment

How do I test failure of assignment to a property?

Note: Python 2.6, I know unittest has some new features in 2.7

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:58:12+00:00Added an answer on June 10, 2026 at 4:58 am

    When you want to use unittest to test that an exception occurs in a block of code rather than just a function call, you can use assertRaises as a context manager:

    with self.assertRaises(PositionError):
        pos.offset = 0
    

    This use can be found in the unittest docs.

    While this is not available in Python 2.6, and won’t work for you as such (I just saw your note), I think it’s worth including among the answers, as it’s probably the clearer way to do this for python 2.7+.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting attributes using minidom in Python, one uses the attributes property. e.g. node.attributes[id].value So
I have an entity in core data that has 2 Attributes. One that is
Is it possible to import the attributes of one table, then I put it
I have a Employee class with many attributes. One of the attributes is employeeId
I have 2 canvases, one uses HTML attributes width and height to size it,
I have one table having ID and other attributes. How can I get list
I'm trying to use quoted text in one of node's attributes: var network_json =
Say I have an ActiveResource which has an schema with several attributes. One of
I have a Project model and it has some text attributes, one is summary.
In my 'Sectioned' UITableView I have two sections, the first one for Attributes like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.