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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:25:42+00:00 2026-05-28T02:25:42+00:00

I have a bunch of modules and for each module I have a unittest

  • 0

I have a bunch of modules and for each module I have a unittest based test. I want to define the main in each module to run its tests, but I get import errors because of import loops (specifically when I use from mymodule import myclass in the test.
I suspect this is a solved problem, so – what should I put in my module’s main to run its corresponding test?

  • 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-28T02:25:43+00:00Added an answer on May 28, 2026 at 2:25 am

    If I understand you correctly, you’ve got a file (lets call it mymodule.py) that looks like this:

    import unittest
    from mymoduletests import MyModuleTests
    
    class myclass(object):
        def somefunction(self, x):
            return x*x
    
    if __name__ == '__main__':
        unittest.main()  
    

    and a separate file (lets call it mymoduletests.py) that looks something like this:

    import unittest
    from mymodule import myclass
    
    class MyModuleTests(unittest.TestCase):
        def test_somefunction(self):
            m = myclass()
            self.assertEqual(4, m.somefunction(2))
    

    If you run mymodule.py you get the following result:

    Traceback (most recent call last):
      File "mymodule.py", line 2, in <module>
        from mymoduletests import MyModuleTests
      File "/Users/srgerg/Desktop/p/mymoduletests.py", line 2, in <module>
        from mymodule import myclass
      File "/Users/srgerg/Desktop/p/mymodule.py", line 2, in <module>
        from mymoduletests import MyModuleTests
    ImportError: cannot import name MyModuleTests
    

    However, if you change mymodule.py to this:

    class myclass(object):
        def somefunction(self, x):
            return x*x
    
    if __name__ == '__main__':
        import unittest
        from mymoduletests import MyModuleTests
        unittest.main()
    

    and then run it, you get:

    .
    ----------------------------------------------------------------------
    Ran 1 test in 0.000s
    
    OK
    

    Have I understood you correctly?

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

Sidebar

Related Questions

I have this module pattern that stores a bunch of vars. I want to
I have a bunch of Python modules I want to clean up, reorganize and
So I have a parent module with a bunch of sub-modules, and I'm trying
I have a bunch of keys that each have an unlikeliness variable. I want
I have a bunch of strings that are dependent on static dictionaries and each
Rails loads controllers, helpers and models on each request. My controllers have a bunch
I am using NHibernate with NHibernate.Linq, and have a bunch of dynamically loading modules
I have a bunch of modules that export an IModule interface. So in the
I have a bunch of categories and items in Joomla and I want one
At my company we have several pseudo-independent teams, owning several repositories each (but sometimes

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.