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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:28:28+00:00 2026-05-21T21:28:28+00:00

Say I have a module with the following: def main(): pass if __name__ ==

  • 0

Say I have a module with the following:

def main():
    pass

if __name__ == "__main__":
    main()

I want to write a unit test for the bottom half (I’d like to achieve 100% coverage). I discovered the runpy builtin module that performs the import/__name__-setting mechanism, but I can’t figure out how to mock or otherwise check that the main() function is called.

This is what I’ve tried so far:

import runpy
import mock

@mock.patch('foobar.main')
def test_main(self, main):
    runpy.run_module('foobar', run_name='__main__')
    main.assert_called_once_with()
  • 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-21T21:28:29+00:00Added an answer on May 21, 2026 at 9:28 pm

    I will choose another alternative which is to exclude the if __name__ == '__main__' from the coverage report , of course you can do that only if you already have a test case for your main() function in your tests.

    As for why I choose to exclude rather than writing a new test case for the whole script is because if as I stated you already have a test case for your main() function the fact that you add an other test case for the script (just for having a 100 % coverage) will be just a duplicated one.

    For how to exclude the if __name__ == '__main__' you can write a coverage configuration file and add in the section report:

    [report]
    
    exclude_lines =
        if __name__ == .__main__.:
    

    More info about the coverage configuration file can be found here.

    Hope this can help.

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

Sidebar

Related Questions

Let's say I have a module mod_x like the following: class X: pass x=X()
Let's say we have code looks like this module TM def aa puts aa
Say i have the following XML: <app-deployment> <name>gr1</name> <target>AdminServer</target> <module-type>ear</module-type> <source-path>/u01/app/wls1035_homes/wls1035_9999/grc864</source-path> <security-dd-model>DDOnly</security-dd-model> <staging-mode>stage</staging-mode> </app-deployment>
Let's say I have a file called example.py with the following content: def func_one(a):
Let's say I have the following tables: class Module { long MID // PK
A simple question on a python module. Let's say I have the following code:
I want the Node everyauth module to work locally with Twitter (say). (I have
Let's say that I have a module that has a Queue in it. For
Let us say if I have a Perl module Resounces.pm with this code snippet
Say I have a Telerik MVC Grid, AJAX bound and I want to put

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.