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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:49:49+00:00 2026-05-30T01:49:49+00:00

I am very new to unit testing and writing/using exceptions. I am currently making

  • 0

I am very new to unit testing and writing/using exceptions. I am currently making a huge effort to learning about best practices and integrating them into my projects. As a test of some things I have been reading about I wrote a simple Contracts module. Below is the init of the contract class which has several arguments that depend on each other.

How would/should I write a test for the init method based on its argument dependencies.

Thanks in advance!

def __init__(self, code, description ,contract_type,
             start_date ,end_date ,reminder_date, 
             customer=None, isgroup=False, vendor=None, 
             discount_perc=None):

    contract_types = ['item','vendor']
    self.code = code
    self.description = description
    self.contract_type = contract_type
    self.start_date = start_date
    self.end_date = end_date
    self.reminder_date = reminder_date
    if contract_type not in contract_types:
        raise AttributeError("Valid contract types are 'item' & 'vendor'")
    if isgroup:
        if customer:
            raise AttributeError("Group contracts should not have 'customer' passed in")
        self.is_group_contract = True
    else:
        if customer:
            self.add_customer(customer)
        else:
            raise AttributeError('Customer required for non group contracts.')
    if contract_type == 'vendor':
        if vendor and discount_perc:
            self.vendor = vendor
            self.discount_perc = discount_perc
        else:
            if not vendor:
                raise AttributeError('Vendor contracts require vendor to be passed in')
            if not discount_perc:
                raise AttributeError('Vendor contracts require discount_perc(Decimal)')

If this type of question isn’t a good fit for SO, where might I be better of going?

  • 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-30T01:49:49+00:00Added an answer on May 30, 2026 at 1:49 am

    I’d treat __init__ similar to any other (not class- or static-) method – test expected output based on various input combinations. But in addition to that, I’d also test it for returning (or not returning, depending on the requirements you have) singleton object.
    However one may prefer to extract singleton tests as the __new__-related test cases.

    Eventually you will have tests for:

    1. Invalid arguments’ types handling (empty/not empty strings, integers, tuples, dicts, etc.).
    2. Invalid arguments combinations handling (in you case it’s risen exceptions).
    3. Optional arguments presence/absence handling (default values work, custom ones do too, etc.).
    4. Valid arguments combinations handling (positive flow works).
    5. Resulting object’s attributes presence/absence and their values (most certainly you rely on them in other methods).
    6. Resulting object being singleton (or not).
    7. ???

    Another tip: extracting contract_types = ['item','vendor'] to the class attribute will help in business logic tests organization.

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

Sidebar

Related Questions

My company is fairly new to unit testing our code. I've been reading about
I am very new to the whole unit testing concept so I'm sorry if
Using Visual Studio 2008 / C# / VS Unit Testing. I have a very
I am very new to unit testing even though i have been coding for
I am very new to unit testing when it comes to databases and especially
I'm trying to retrieve some information about my hard drives using PowerShell (very new
Very new to FluentNHibernate, but I'm also excited about the area. I've recently started
I still very new using Subversion. Is it possible to have a working copy
I'm pretty new to IoC, Dependency Injection and Unit Testing. I'm starting a new
What object types are best for writing unit tests for generic collections? Obviously if

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.