omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    DocTests in seperate file not working

    Pythonista
    2
    5
    2226
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • shfonic
      shfonic last edited by

      Hi, I'm having issues getting the DocTests to run as a seperate file using the built in DocTests feature (creating a file name.doctest file and pressing play). The DocTest file works when calling existing packages, but if I do the following:
      New top-level folder called "HelloWorld"
      New script called "helloworld.py" with the following code:

      def get_hello(name):
          return "Hello " + name
      

      Then create a "helloworld.doctest" with the following:

      >>> import helloworld
      >>> helloworld.get_hello("Richard")
      'Hello Richard'
      

      Then run the DocTest by pressing play (pressing play using the default template passes), I get the following error:

      ImportError: No module named 'helloworld'
      NameError: name 'helloworld' is not defined
      

      If I add the DocTest into the comment of the method, running DocTest works on that script. But I want to remove clutter and have the tests in a seperate file and use this feature.

      def get_hello(name):
          """
          >>> get_hello("Richard")
          'Hello Richard'
          """
          return "Hello " + name
      

      Is there something I'm missing? Any help will be most appreciated

      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        IIRC, the issue is that doctests don't get sys.path modified the way real scripts do.

        You should be able to add sys.path.append(yourpathname) and it should work.
        Unfortunately, file is also not defined, so you cannot easily just use relative paths.

        1 Reply Last reply Reply Quote 1
        • shfonic
          shfonic last edited by

          Thanks JonB. The testing of the get_hello function works. However the test fails on the line of the import helloworld. It says Got: (the info of sys.path). This does not stop me testing my code now, however it just has one failure due to this. Any thoughts? Thanks again.

          >>> import sys; _path = '/private/var/mobile/Containers/Shared/AppGroup/.../Pythonista3/Documents/HelloWorld'; sys.path.append(_path)
          
          
          >>> import helloworld
          >>> helloworld.get_hello("Richard")
          'Hello Richard'
          
          1 Reply Last reply Reply Quote 0
          • shfonic
            shfonic last edited by

            I created a DocTest file for the actual module I want to test and weirdly I don't get the import issue... I don't see any difference except the module I'm testing is two folder layers deep (.../Apps/AppName/appname.py rather than .../HelloWorld/helloworld.py). But as it's fully working with the module I actually want to test, that's fine for me for now.

            1 Reply Last reply Reply Quote 0
            • JonB
              JonB last edited by

              If you just ran the .py file, the folder will already be in sys.path.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Powered by NodeBB Forums | Contributors