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 8210441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:04:21+00:00 2026-06-07T10:04:21+00:00

I am trying to assertRaise the exception inside a function where a condition raises

  • 0

I am trying to assertRaise the exception inside a function where a condition raises a custom exception message .

Function:

if not Cart.objects.filter(member=member).count():
    raise CartDoesNotExist("Cart Does Not Exist for Member: %s ( %id )." % (member.email,member.id))

Now , i am able to successfully produce the required condition to get to the raise statement.

So , my testcase looks like this :

def Order_CartDoesNotExist(self):        
  self.assertRaises(CartDoesNotExist,Order.objects.create_order(member=self.member2,member_slot=self.memslot,order_type="Normal"))

When i run the test , the output is an Error . It gives the same error CartDoesNotExist…..

So my question is , how to raise these kind of exceptions ? How to cover these situations in our unittest? I do not want to escape these conditions as they are important and increase code coverage?

Thank you all.

  • 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-07T10:04:22+00:00Added an answer on June 7, 2026 at 10:04 am

    Your code is calling create_order directly, which raises the exception. You need to change how it is called. In Python 2.7, you can use this:

    with self.assertRaises(CartDoesNotExist):
        Order.objects.create_order(member=self.member2, member_slot=self.memslot, order_type="Normal"))
    

    Here the context manager allows you to call your code directly, and the context manager will handle the exception for you.

    If you are running with 2.6 or below:

    self.assertRaises(CartDoesNotExist, Order.objects.create_order, member=self.member2, member_slot=self.memslot, order_type="Normal")
    

    Here you aren’t calling your function, you are passing it to assertRaises, along with the arguments it needs, and assertRaises will call the code and deal with the exception properly.

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

Sidebar

Related Questions

Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Trying to import a custom UITableViewCell and it keeps giving me a file not
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
I am trying to test for an exception. I have: def test_set_catch_status_exception(self): mro =
I am trying to implement password based encryption algorithm, but I get this exception:
Trying to use log4j's built-in JMSAppender to log messages to a local message queue
I'm trying to run this test: self.assertRaises(AttributeError, branch[0].childrennodes) , and branch[0 ] does not
Trying to display a message if no results found in the search query. Here
I am ultimately trying to unit test a custom base adapter, but I am
trying to work out how to install zipArchive extension with MAMP, but not having

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.