How do I write Python unit tests for scripts in my bin directory











up vote
7
down vote

favorite
3












The Python unittest module seems to assume a directory structure for a project in which there's a project root level directory with the source code and tests under that directory.



I would like, however, to write Python scripts in my ~/bin directory and tests for it in another directory (say, ~/dev/tests). Is there a way for me to run the unit tests using the command line interface without setting my PYTHONPATH environment variable and creating __init__.py files and whatnot?



Here's a simple example demonstrating what I want:



~/bin/candy:



#!/usr/bin/env python

def candy():
return "candy"

if __name__ == '__main__':
print candy()


~/dev/tests/test_candy.py:



#!/usr/bin/env python

import unittest
import candy

class CandyTestCase(unittest.TestCase):

def testCandy(self):
candyOutput = candy.candy()

assert candyOutput == "candy"


I notice that everything can be done conveniently if:




  • The two files are named with py extensions (candy.py and test_candy.py)

  • The two files are in the same directory

  • The test is run with the following in the directory of the tests:
    $ python -m unittest test_candy


Can I run python with the unittest module to do the following without setting anything in my environment explicitly:




  • My file under test does not have the py extension (just ~/candy).

  • I don't care if test_candy has py as an extension or not.

  • I want candy and test_candy.py to not share a common root (other than my home directory).


If that's not possible with a simple invocation of python -m unittest, what is the most simple way to accomplish this?










share|improve this question






















  • Why are you writing python files without a .py extension?
    – ppperry
    Nov 2 '15 at 1:27






  • 4




    Modules in a python library should have the .py extension. Executable scripts, on the other hand, be they shell, perl, python, or whatever, don't need .ksh, .bash, .py, etc. extensions because the user of them doesn't need to know how what language they are implemented in to run them. I generally don't name executables with an extension indicating the type of script it is.
    – firebush
    Nov 3 '15 at 2:51










  • It's pretty easy to do this for adhoc tests, just use imp.load_source(MODULE_NAME, 'bin/EXECUTABLE_FILE'), what I find I'm having trouble with is when doing debuild, it's not copying files that don't end in .py to the build dir so those scripts don't get tested in the build
    – Peter Turner
    Nov 8 at 19:55










  • @PeterTurner can you elaborate a bit more? What kind of build are you performing? If it's the distutils/setuptools build, then the scripts passed via scripts list to the setup function are copied to build/scripts-X.Y, where X.Y is the interpreter version.
    – hoefling
    Nov 10 at 23:52










  • @hoefling, oh, I don't have a "scripts" list I'll have to look that up, I'm just doing a debuild with some parameters, can't remember off-hand, I'll look it up Monday
    – Peter Turner
    Nov 11 at 18:24















up vote
7
down vote

favorite
3












The Python unittest module seems to assume a directory structure for a project in which there's a project root level directory with the source code and tests under that directory.



I would like, however, to write Python scripts in my ~/bin directory and tests for it in another directory (say, ~/dev/tests). Is there a way for me to run the unit tests using the command line interface without setting my PYTHONPATH environment variable and creating __init__.py files and whatnot?



Here's a simple example demonstrating what I want:



~/bin/candy:



#!/usr/bin/env python

def candy():
return "candy"

if __name__ == '__main__':
print candy()


~/dev/tests/test_candy.py:



#!/usr/bin/env python

import unittest
import candy

class CandyTestCase(unittest.TestCase):

def testCandy(self):
candyOutput = candy.candy()

assert candyOutput == "candy"


I notice that everything can be done conveniently if:




  • The two files are named with py extensions (candy.py and test_candy.py)

  • The two files are in the same directory

  • The test is run with the following in the directory of the tests:
    $ python -m unittest test_candy


Can I run python with the unittest module to do the following without setting anything in my environment explicitly:




  • My file under test does not have the py extension (just ~/candy).

  • I don't care if test_candy has py as an extension or not.

  • I want candy and test_candy.py to not share a common root (other than my home directory).


If that's not possible with a simple invocation of python -m unittest, what is the most simple way to accomplish this?










share|improve this question






















  • Why are you writing python files without a .py extension?
    – ppperry
    Nov 2 '15 at 1:27






  • 4




    Modules in a python library should have the .py extension. Executable scripts, on the other hand, be they shell, perl, python, or whatever, don't need .ksh, .bash, .py, etc. extensions because the user of them doesn't need to know how what language they are implemented in to run them. I generally don't name executables with an extension indicating the type of script it is.
    – firebush
    Nov 3 '15 at 2:51










  • It's pretty easy to do this for adhoc tests, just use imp.load_source(MODULE_NAME, 'bin/EXECUTABLE_FILE'), what I find I'm having trouble with is when doing debuild, it's not copying files that don't end in .py to the build dir so those scripts don't get tested in the build
    – Peter Turner
    Nov 8 at 19:55










  • @PeterTurner can you elaborate a bit more? What kind of build are you performing? If it's the distutils/setuptools build, then the scripts passed via scripts list to the setup function are copied to build/scripts-X.Y, where X.Y is the interpreter version.
    – hoefling
    Nov 10 at 23:52










  • @hoefling, oh, I don't have a "scripts" list I'll have to look that up, I'm just doing a debuild with some parameters, can't remember off-hand, I'll look it up Monday
    – Peter Turner
    Nov 11 at 18:24













up vote
7
down vote

favorite
3









up vote
7
down vote

favorite
3






3





The Python unittest module seems to assume a directory structure for a project in which there's a project root level directory with the source code and tests under that directory.



I would like, however, to write Python scripts in my ~/bin directory and tests for it in another directory (say, ~/dev/tests). Is there a way for me to run the unit tests using the command line interface without setting my PYTHONPATH environment variable and creating __init__.py files and whatnot?



Here's a simple example demonstrating what I want:



~/bin/candy:



#!/usr/bin/env python

def candy():
return "candy"

if __name__ == '__main__':
print candy()


~/dev/tests/test_candy.py:



#!/usr/bin/env python

import unittest
import candy

class CandyTestCase(unittest.TestCase):

def testCandy(self):
candyOutput = candy.candy()

assert candyOutput == "candy"


I notice that everything can be done conveniently if:




  • The two files are named with py extensions (candy.py and test_candy.py)

  • The two files are in the same directory

  • The test is run with the following in the directory of the tests:
    $ python -m unittest test_candy


Can I run python with the unittest module to do the following without setting anything in my environment explicitly:




  • My file under test does not have the py extension (just ~/candy).

  • I don't care if test_candy has py as an extension or not.

  • I want candy and test_candy.py to not share a common root (other than my home directory).


If that's not possible with a simple invocation of python -m unittest, what is the most simple way to accomplish this?










share|improve this question













The Python unittest module seems to assume a directory structure for a project in which there's a project root level directory with the source code and tests under that directory.



I would like, however, to write Python scripts in my ~/bin directory and tests for it in another directory (say, ~/dev/tests). Is there a way for me to run the unit tests using the command line interface without setting my PYTHONPATH environment variable and creating __init__.py files and whatnot?



Here's a simple example demonstrating what I want:



~/bin/candy:



#!/usr/bin/env python

def candy():
return "candy"

if __name__ == '__main__':
print candy()


~/dev/tests/test_candy.py:



#!/usr/bin/env python

import unittest
import candy

class CandyTestCase(unittest.TestCase):

def testCandy(self):
candyOutput = candy.candy()

assert candyOutput == "candy"


I notice that everything can be done conveniently if:




  • The two files are named with py extensions (candy.py and test_candy.py)

  • The two files are in the same directory

  • The test is run with the following in the directory of the tests:
    $ python -m unittest test_candy


Can I run python with the unittest module to do the following without setting anything in my environment explicitly:




  • My file under test does not have the py extension (just ~/candy).

  • I don't care if test_candy has py as an extension or not.

  • I want candy and test_candy.py to not share a common root (other than my home directory).


If that's not possible with a simple invocation of python -m unittest, what is the most simple way to accomplish this?







python unit-testing python-unittest






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 2 '15 at 1:12









firebush

1,26431320




1,26431320












  • Why are you writing python files without a .py extension?
    – ppperry
    Nov 2 '15 at 1:27






  • 4




    Modules in a python library should have the .py extension. Executable scripts, on the other hand, be they shell, perl, python, or whatever, don't need .ksh, .bash, .py, etc. extensions because the user of them doesn't need to know how what language they are implemented in to run them. I generally don't name executables with an extension indicating the type of script it is.
    – firebush
    Nov 3 '15 at 2:51










  • It's pretty easy to do this for adhoc tests, just use imp.load_source(MODULE_NAME, 'bin/EXECUTABLE_FILE'), what I find I'm having trouble with is when doing debuild, it's not copying files that don't end in .py to the build dir so those scripts don't get tested in the build
    – Peter Turner
    Nov 8 at 19:55










  • @PeterTurner can you elaborate a bit more? What kind of build are you performing? If it's the distutils/setuptools build, then the scripts passed via scripts list to the setup function are copied to build/scripts-X.Y, where X.Y is the interpreter version.
    – hoefling
    Nov 10 at 23:52










  • @hoefling, oh, I don't have a "scripts" list I'll have to look that up, I'm just doing a debuild with some parameters, can't remember off-hand, I'll look it up Monday
    – Peter Turner
    Nov 11 at 18:24


















  • Why are you writing python files without a .py extension?
    – ppperry
    Nov 2 '15 at 1:27






  • 4




    Modules in a python library should have the .py extension. Executable scripts, on the other hand, be they shell, perl, python, or whatever, don't need .ksh, .bash, .py, etc. extensions because the user of them doesn't need to know how what language they are implemented in to run them. I generally don't name executables with an extension indicating the type of script it is.
    – firebush
    Nov 3 '15 at 2:51










  • It's pretty easy to do this for adhoc tests, just use imp.load_source(MODULE_NAME, 'bin/EXECUTABLE_FILE'), what I find I'm having trouble with is when doing debuild, it's not copying files that don't end in .py to the build dir so those scripts don't get tested in the build
    – Peter Turner
    Nov 8 at 19:55










  • @PeterTurner can you elaborate a bit more? What kind of build are you performing? If it's the distutils/setuptools build, then the scripts passed via scripts list to the setup function are copied to build/scripts-X.Y, where X.Y is the interpreter version.
    – hoefling
    Nov 10 at 23:52










  • @hoefling, oh, I don't have a "scripts" list I'll have to look that up, I'm just doing a debuild with some parameters, can't remember off-hand, I'll look it up Monday
    – Peter Turner
    Nov 11 at 18:24
















Why are you writing python files without a .py extension?
– ppperry
Nov 2 '15 at 1:27




Why are you writing python files without a .py extension?
– ppperry
Nov 2 '15 at 1:27




4




4




Modules in a python library should have the .py extension. Executable scripts, on the other hand, be they shell, perl, python, or whatever, don't need .ksh, .bash, .py, etc. extensions because the user of them doesn't need to know how what language they are implemented in to run them. I generally don't name executables with an extension indicating the type of script it is.
– firebush
Nov 3 '15 at 2:51




Modules in a python library should have the .py extension. Executable scripts, on the other hand, be they shell, perl, python, or whatever, don't need .ksh, .bash, .py, etc. extensions because the user of them doesn't need to know how what language they are implemented in to run them. I generally don't name executables with an extension indicating the type of script it is.
– firebush
Nov 3 '15 at 2:51












It's pretty easy to do this for adhoc tests, just use imp.load_source(MODULE_NAME, 'bin/EXECUTABLE_FILE'), what I find I'm having trouble with is when doing debuild, it's not copying files that don't end in .py to the build dir so those scripts don't get tested in the build
– Peter Turner
Nov 8 at 19:55




It's pretty easy to do this for adhoc tests, just use imp.load_source(MODULE_NAME, 'bin/EXECUTABLE_FILE'), what I find I'm having trouble with is when doing debuild, it's not copying files that don't end in .py to the build dir so those scripts don't get tested in the build
– Peter Turner
Nov 8 at 19:55












@PeterTurner can you elaborate a bit more? What kind of build are you performing? If it's the distutils/setuptools build, then the scripts passed via scripts list to the setup function are copied to build/scripts-X.Y, where X.Y is the interpreter version.
– hoefling
Nov 10 at 23:52




@PeterTurner can you elaborate a bit more? What kind of build are you performing? If it's the distutils/setuptools build, then the scripts passed via scripts list to the setup function are copied to build/scripts-X.Y, where X.Y is the interpreter version.
– hoefling
Nov 10 at 23:52












@hoefling, oh, I don't have a "scripts" list I'll have to look that up, I'm just doing a debuild with some parameters, can't remember off-hand, I'll look it up Monday
– Peter Turner
Nov 11 at 18:24




@hoefling, oh, I don't have a "scripts" list I'll have to look that up, I'm just doing a debuild with some parameters, can't remember off-hand, I'll look it up Monday
– Peter Turner
Nov 11 at 18:24












3 Answers
3






active

oldest

votes

















up vote
3
down vote



+100










This is candy executable (no change):



➜ cat ~/bin/candy

#!/usr/bin/env python
def candy():
return "candy"

if __name__ == '__main__':
print candy()


and this is ~/dev/tests/test_candy.py (changed):



➜ cat ~/dev/tests/test_candy.py

#!/usr/bin/env python

import imp
import unittest

from os.path import expanduser, join

# use expanduser to locate its home dir and join bin and candy module paths
candy_module_path = join(expanduser("~"), "bin", "candy")

# load the module without .py extension
candy = imp.load_source("candy", candy_module_path)


class CandyTestCase(unittest.TestCase):

def testCandy(self):
candyOutput = candy.candy()

assert candyOutput == "candy"


What changed?




  • We added imp.load_source to import ~/bin/candy (a module without *.py extension)


  • We added provision to locate home directory mention i.e. ~ using expanduser


  • We are using os.path.join to join the paths for ~/bin/candy



Now you can run the tests with discover option of unittest module.



Check python -m unittest --help for more details.



Excerpts below



-s directory Directory to start discovery ('.' default)



-p pattern Pattern to match test files ('test*.py' default)



➜ python -m unittest discover -s ~/bin/ -p 'test*' -v ~/dev/tests
testCandy (test_candy.CandyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK





share|improve this answer




























    up vote
    0
    down vote













    I have not tried this with unittest, but my quick fix for problems like these are to just change my working directory inside the script using the os module. This SHOULD work for you though.



    #!/usr/bin/env python

    import unittest
    import os
    os.chdir("/usr/bin/candy")
    import candy

    class CandyTestCase(unittest.TestCase):

    def testCandy(self):
    candyOutput = candy.candy()

    assert candyOutput == "candy"





    share|improve this answer




























      up vote
      0
      down vote













      Short answer, no. The problem is that you need to import the modules you're trying to test, so at least you have to change your PYTHONPATH. My advice is that you change it temporarily as you execute the tests, like this:



      import sys
      sys.path.extend(['~/dir1','~/dir2','~/anotherdir'])


      The solution of @Paritosh_Singh is algo good.



      The long run is to install a test runner like tox and configure it so it "sees" your modules. But I think you don't want to do that.






      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33469246%2fhow-do-i-write-python-unit-tests-for-scripts-in-my-bin-directory%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        3
        down vote



        +100










        This is candy executable (no change):



        ➜ cat ~/bin/candy

        #!/usr/bin/env python
        def candy():
        return "candy"

        if __name__ == '__main__':
        print candy()


        and this is ~/dev/tests/test_candy.py (changed):



        ➜ cat ~/dev/tests/test_candy.py

        #!/usr/bin/env python

        import imp
        import unittest

        from os.path import expanduser, join

        # use expanduser to locate its home dir and join bin and candy module paths
        candy_module_path = join(expanduser("~"), "bin", "candy")

        # load the module without .py extension
        candy = imp.load_source("candy", candy_module_path)


        class CandyTestCase(unittest.TestCase):

        def testCandy(self):
        candyOutput = candy.candy()

        assert candyOutput == "candy"


        What changed?




        • We added imp.load_source to import ~/bin/candy (a module without *.py extension)


        • We added provision to locate home directory mention i.e. ~ using expanduser


        • We are using os.path.join to join the paths for ~/bin/candy



        Now you can run the tests with discover option of unittest module.



        Check python -m unittest --help for more details.



        Excerpts below



        -s directory Directory to start discovery ('.' default)



        -p pattern Pattern to match test files ('test*.py' default)



        ➜ python -m unittest discover -s ~/bin/ -p 'test*' -v ~/dev/tests
        testCandy (test_candy.CandyTestCase) ... ok

        ----------------------------------------------------------------------
        Ran 1 test in 0.000s

        OK





        share|improve this answer

























          up vote
          3
          down vote



          +100










          This is candy executable (no change):



          ➜ cat ~/bin/candy

          #!/usr/bin/env python
          def candy():
          return "candy"

          if __name__ == '__main__':
          print candy()


          and this is ~/dev/tests/test_candy.py (changed):



          ➜ cat ~/dev/tests/test_candy.py

          #!/usr/bin/env python

          import imp
          import unittest

          from os.path import expanduser, join

          # use expanduser to locate its home dir and join bin and candy module paths
          candy_module_path = join(expanduser("~"), "bin", "candy")

          # load the module without .py extension
          candy = imp.load_source("candy", candy_module_path)


          class CandyTestCase(unittest.TestCase):

          def testCandy(self):
          candyOutput = candy.candy()

          assert candyOutput == "candy"


          What changed?




          • We added imp.load_source to import ~/bin/candy (a module without *.py extension)


          • We added provision to locate home directory mention i.e. ~ using expanduser


          • We are using os.path.join to join the paths for ~/bin/candy



          Now you can run the tests with discover option of unittest module.



          Check python -m unittest --help for more details.



          Excerpts below



          -s directory Directory to start discovery ('.' default)



          -p pattern Pattern to match test files ('test*.py' default)



          ➜ python -m unittest discover -s ~/bin/ -p 'test*' -v ~/dev/tests
          testCandy (test_candy.CandyTestCase) ... ok

          ----------------------------------------------------------------------
          Ran 1 test in 0.000s

          OK





          share|improve this answer























            up vote
            3
            down vote



            +100







            up vote
            3
            down vote



            +100




            +100




            This is candy executable (no change):



            ➜ cat ~/bin/candy

            #!/usr/bin/env python
            def candy():
            return "candy"

            if __name__ == '__main__':
            print candy()


            and this is ~/dev/tests/test_candy.py (changed):



            ➜ cat ~/dev/tests/test_candy.py

            #!/usr/bin/env python

            import imp
            import unittest

            from os.path import expanduser, join

            # use expanduser to locate its home dir and join bin and candy module paths
            candy_module_path = join(expanduser("~"), "bin", "candy")

            # load the module without .py extension
            candy = imp.load_source("candy", candy_module_path)


            class CandyTestCase(unittest.TestCase):

            def testCandy(self):
            candyOutput = candy.candy()

            assert candyOutput == "candy"


            What changed?




            • We added imp.load_source to import ~/bin/candy (a module without *.py extension)


            • We added provision to locate home directory mention i.e. ~ using expanduser


            • We are using os.path.join to join the paths for ~/bin/candy



            Now you can run the tests with discover option of unittest module.



            Check python -m unittest --help for more details.



            Excerpts below



            -s directory Directory to start discovery ('.' default)



            -p pattern Pattern to match test files ('test*.py' default)



            ➜ python -m unittest discover -s ~/bin/ -p 'test*' -v ~/dev/tests
            testCandy (test_candy.CandyTestCase) ... ok

            ----------------------------------------------------------------------
            Ran 1 test in 0.000s

            OK





            share|improve this answer












            This is candy executable (no change):



            ➜ cat ~/bin/candy

            #!/usr/bin/env python
            def candy():
            return "candy"

            if __name__ == '__main__':
            print candy()


            and this is ~/dev/tests/test_candy.py (changed):



            ➜ cat ~/dev/tests/test_candy.py

            #!/usr/bin/env python

            import imp
            import unittest

            from os.path import expanduser, join

            # use expanduser to locate its home dir and join bin and candy module paths
            candy_module_path = join(expanduser("~"), "bin", "candy")

            # load the module without .py extension
            candy = imp.load_source("candy", candy_module_path)


            class CandyTestCase(unittest.TestCase):

            def testCandy(self):
            candyOutput = candy.candy()

            assert candyOutput == "candy"


            What changed?




            • We added imp.load_source to import ~/bin/candy (a module without *.py extension)


            • We added provision to locate home directory mention i.e. ~ using expanduser


            • We are using os.path.join to join the paths for ~/bin/candy



            Now you can run the tests with discover option of unittest module.



            Check python -m unittest --help for more details.



            Excerpts below



            -s directory Directory to start discovery ('.' default)



            -p pattern Pattern to match test files ('test*.py' default)



            ➜ python -m unittest discover -s ~/bin/ -p 'test*' -v ~/dev/tests
            testCandy (test_candy.CandyTestCase) ... ok

            ----------------------------------------------------------------------
            Ran 1 test in 0.000s

            OK






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 at 7:47









            Navid

            487312




            487312
























                up vote
                0
                down vote













                I have not tried this with unittest, but my quick fix for problems like these are to just change my working directory inside the script using the os module. This SHOULD work for you though.



                #!/usr/bin/env python

                import unittest
                import os
                os.chdir("/usr/bin/candy")
                import candy

                class CandyTestCase(unittest.TestCase):

                def testCandy(self):
                candyOutput = candy.candy()

                assert candyOutput == "candy"





                share|improve this answer

























                  up vote
                  0
                  down vote













                  I have not tried this with unittest, but my quick fix for problems like these are to just change my working directory inside the script using the os module. This SHOULD work for you though.



                  #!/usr/bin/env python

                  import unittest
                  import os
                  os.chdir("/usr/bin/candy")
                  import candy

                  class CandyTestCase(unittest.TestCase):

                  def testCandy(self):
                  candyOutput = candy.candy()

                  assert candyOutput == "candy"





                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    I have not tried this with unittest, but my quick fix for problems like these are to just change my working directory inside the script using the os module. This SHOULD work for you though.



                    #!/usr/bin/env python

                    import unittest
                    import os
                    os.chdir("/usr/bin/candy")
                    import candy

                    class CandyTestCase(unittest.TestCase):

                    def testCandy(self):
                    candyOutput = candy.candy()

                    assert candyOutput == "candy"





                    share|improve this answer












                    I have not tried this with unittest, but my quick fix for problems like these are to just change my working directory inside the script using the os module. This SHOULD work for you though.



                    #!/usr/bin/env python

                    import unittest
                    import os
                    os.chdir("/usr/bin/candy")
                    import candy

                    class CandyTestCase(unittest.TestCase):

                    def testCandy(self):
                    candyOutput = candy.candy()

                    assert candyOutput == "candy"






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 10 at 20:59









                    Paritosh Singh

                    2116




                    2116






















                        up vote
                        0
                        down vote













                        Short answer, no. The problem is that you need to import the modules you're trying to test, so at least you have to change your PYTHONPATH. My advice is that you change it temporarily as you execute the tests, like this:



                        import sys
                        sys.path.extend(['~/dir1','~/dir2','~/anotherdir'])


                        The solution of @Paritosh_Singh is algo good.



                        The long run is to install a test runner like tox and configure it so it "sees" your modules. But I think you don't want to do that.






                        share|improve this answer

























                          up vote
                          0
                          down vote













                          Short answer, no. The problem is that you need to import the modules you're trying to test, so at least you have to change your PYTHONPATH. My advice is that you change it temporarily as you execute the tests, like this:



                          import sys
                          sys.path.extend(['~/dir1','~/dir2','~/anotherdir'])


                          The solution of @Paritosh_Singh is algo good.



                          The long run is to install a test runner like tox and configure it so it "sees" your modules. But I think you don't want to do that.






                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Short answer, no. The problem is that you need to import the modules you're trying to test, so at least you have to change your PYTHONPATH. My advice is that you change it temporarily as you execute the tests, like this:



                            import sys
                            sys.path.extend(['~/dir1','~/dir2','~/anotherdir'])


                            The solution of @Paritosh_Singh is algo good.



                            The long run is to install a test runner like tox and configure it so it "sees" your modules. But I think you don't want to do that.






                            share|improve this answer












                            Short answer, no. The problem is that you need to import the modules you're trying to test, so at least you have to change your PYTHONPATH. My advice is that you change it temporarily as you execute the tests, like this:



                            import sys
                            sys.path.extend(['~/dir1','~/dir2','~/anotherdir'])


                            The solution of @Paritosh_Singh is algo good.



                            The long run is to install a test runner like tox and configure it so it "sees" your modules. But I think you don't want to do that.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 14 at 1:01









                            PROW

                            1427




                            1427






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33469246%2fhow-do-i-write-python-unit-tests-for-scripts-in-my-bin-directory%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                The Sandy Post

                                Danny Elfman

                                Pages that link to "Head v. Amoskeag Manufacturing Co."