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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:45:46+00:00 2026-05-22T21:45:46+00:00

In the Python unittest framework, is there a way to pass a unit test

  • 0

In the Python unittest framework, is there a way to pass a unit test if an exception wasn’t raised, and fail with an AssertRaise otherwise?

  • 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-05-22T21:45:47+00:00Added an answer on May 22, 2026 at 9:45 pm

    If I understand your question correctly, you could do something like this:

    def test_does_not_raise_on_valid_input(self):
        raised = False
        try:
            do_something(42)
        except:
            raised = True
        self.assertFalse(raised, 'Exception raised')
    

    …assuming that you have a corresponding test that the correct Exception gets raised on invalid input, of course:

    def test_does_raise_on_invalid_input(self):
        self.assertRaises(OutOfCheese, do_something, 43)
    

    However, as pointed out in the comments, you need to consider what it is that you are actually testing. It’s likely that a test like…

    def test_what_is_42(self):
        self.assertEquals(do_something(42), 'Meaning of life')
    

    …is better because it tests the desired behaviour of the system and will fail if an exception is raised.

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

Sidebar

Related Questions

I am using python unit test module. I am wondering is there anyway to
I used unittest and nose for unit-testing in Python but now I'm using py.test.
I'm doing some Unit/Integration testing in a Non-Java language (Python), using a test-framework (
Is there a way to make python unittest (I am using 2.6, I have
I'm extending the python 2.7 unittest framework to do some function testing. One of
I'm using Python's unittest module for unit testing. I'd like to be able to
I am runninig test's with Python Unittest. I am running tests but I want
I'm writing a testing framework using Python's unittest. I have separate modules for separate
I am trying to do a simple test in Python using ' unittest ',
Possible Duplicate: How do you generate dynamic (parameterized) unit tests in Python? Is there

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.