How to test class methods from outside the class in Python?
Question: I am trying to test each method in a class, from another module. So here is
the class.

    
    
    #newmodule
    
    class test:
        def atest(a,b):
            return a
    
        def btest(a,b):
            return b
    

and in the other module, I am attempting to do:

    
    
    import unittest
    import newmodule
    test.atest(5,4).assert not errors
    test.atest(7,9).assert not errors
    

Note: I'm sure there are all sorts of errors here, but I just mocked this up
as an example. The main question I have here is how to successfully import
newmodule and test each METHOD. I suspect that there are complications with
trying to test methods from outside of the class as opposed to just testing
functions.

I am already failing right off the bat because I am getting:

    
    
    ImportError: no module named newmodule
    

even though they are in the same directory.

How do I successfully import this module and if so, am I able to test the
methods from outside the class?


Answer: _"even though they are in the same directory."_ \- its not whether they are in
the same directory, its whether `newmodule` is in the current working
directory. If you want to run tests from a different directory, you could add
your script directory to `sys.path`

    
    
    import os
    import sys
    sys.path.insert(0, os.path.dirname(__file__))
    import unittest
    import newmodule
    

This hard-codes which version of your module you are going test test so you
have to decide whether that fits your goals. Alternately you could set the
`PYTHONPATH` enivronment variable outside of your test script for more
flexibility.

