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

  • Home
  • SEARCH
  • 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 8067581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:21:50+00:00 2026-06-05T12:21:50+00:00

I started using unitest in my django projects, and am interested in testing process_email()

  • 0

I started using unitest in my django projects, and am interested in testing process_email(). Since I am testing email functionality, I am interested in testing the parse_email() function. This code below is command line management function (i.e. I do python manage.py process_email).

How do I call parse_email() in my unittest?

Since this code is in a file called somemodel/management/commands/process_email.py, only process_email() is visible. Subsequently, call_command('parse_email', mail.outbox[1].body, interactive=False) will not work since only process_email() is visible.

def process_email():
    server = imaplib.IMAP4_SSL(settings.EMAIL_HOST,993)
    server.login(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD)
    server.select('INBOX')
    status, data = server.search(None, 'NOT', 'DELETED')

    if data:
        msgnums = data[0].split()

        for num in msgnums:
            status, data = server.fetch(num, '(RFC822)')
            valid = parse_email(data[0][1])
            print "valid is ", valid
            #            if not valid:
            server.store(num, '+FLAGS', '\\Deleted')

    server.expunge()
    server.close()
    server.logout()

def parse_email(message):
    # 'message' must be an RFC822 formatted message.
    pdb.set_trace()
    try:
        msg = message   
        message = email.message_from_string(msg)
        subject = message.get('subject', _('Created from e-mail'))
        subject = decode_mail_headers(decodeUnknown(message.get_charset(), subject))
        subject = subject.replace("Re: ", "").replace("Fw: ", "").replace("RE: ", "").replace("FW: ", "").strip()

Here is what I mean when I say it is not visible (perhaps the wrong use of the word): When I call call_command('parse_email') in my test file, I get the following error:

======================================================================
ERROR: test_submit_bldg (bldg.tests.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/bldg/tests.py", line 36, in test_submit_bldg
      print call_command('parse_email') 
  File "django/core/management/__init__.py", line 136, in call_command
  raise CommandError("Unknown command: %r" % name)
  CommandError: Unknown command: 'parse_email'
  • 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-05T12:21:51+00:00Added an answer on June 5, 2026 at 12:21 pm

    You can import parse_email directly from your tests:

    from somemodel.management.commands.process_email import parse_email
    assert_equals(parse_email("..."), ...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started using EF 4.1 code first. I have a entity table like this:
I started using LESS today. But it's kinda weird. This code does not work.
Started using Visual Studio 2012 RC since yesterday, We have one WCF solution. Whenever
I started using backbone for my projects. I finished a simple project and it
I started using the Mojolicious library for testing and everything was working fine in
I started using ModX yesterday. Prepared my multilingual website using this tutorial: http://www.multilingual-modx.com/blog/2011/multilingual-websites-with-modx-and-babel.html .
I started using NHibernate this week (struggling). I have a small application with 3
I started using ZeroMQ this week, and when using the Request-Response pattern I am
I started using NoSQL with Ruby. Here's the code I have require 'mongo' require
I've recently started using git, and also begun unit testing (using Python's unittest module).

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.