1

I am first time trying out python unit tests referring to this article. I have PyDev plugin installed in my Eclipse.

My test_hello.py looks like this:

import unittest

class TestHello(unittest.TestCase):
    def test_abc(self):
        print("Test!!!")
        result = True
        self.assertEqual(result, True, "ohno")

When I Right click on source > Run As > Python unit-test, it outputs:

Finding files... done.
Importing test modules ... PYTHONPATH not found for file: D:\workspaces\python-ws\test\test_h
done.

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

Also in PyUnit tab, it doesnt show anything:

enter image description here

What I am missing here?

Update

Adding more details:

My project:

enter image description here

PyDev package explorer

enter image description here

MsA
  • 2,599
  • 3
  • 22
  • 47
  • you define the test, but it doesn't look like you are calling your function - therefore, nothing is executed – Cut7er Oct 01 '18 at 14:45
  • I just tried by adding `a = TestHello()` and `a.test_abc() ` at the end. Still no use. – MsA Oct 01 '18 at 14:48
  • Also am I supposed to do that? In java unit test we just annotate with `@Test` and it works. Also the stated article doesnt seem to do that either... – MsA Oct 01 '18 at 14:49
  • not sure, I have never used this module before - maybe someone else can help – Cut7er Oct 01 '18 at 14:50
  • did u used any other for unit tests? – MsA Oct 01 '18 at 14:50
  • nope, my testing routine is normally just changing the code and looking if it works....not advisable though – Cut7er Oct 01 '18 at 14:52
  • 1
    It looks to me like your setup is not right. It says it can't find `PYTONPATH`. Check your configuration for eclipse to verify that it is configured properly. – Alan Hoover Oct 01 '18 at 14:56
  • This might help http://www.pydev.org/faq.html – mad_ Oct 01 '18 at 15:00
  • ohh could that be an issue...eclipse was shouting that to me for ages now...but my python scripts were always running – MsA Oct 01 '18 at 15:01
  • What is python path? Is it where my python resides? Referring [this guide](http://www.pydev.org/manual_101_interpreter.html). Is it same as path of interpreters I have added? – MsA Oct 01 '18 at 15:07
  • i had a similar problem. Restarting eclipse seemed to fix the problem as a subsequent run as unittest ran the test case. – David Wood Mar 16 '23 at 21:12

1 Answers1

1

The issue is that you have probably not configured which path in your project should be in the PYTHONPATH.

In PyDev terms, this is the source folder for a project.

You should be able to right-click a folder and select PyDev > Set as Source Folder (add to PYTHONPATH) to add that folder to the PYTHONPATH.

There's a better description at: http://www.pydev.org/manual_101_project_conf2.html

Also, make sure that you take a look at the getting started guide: http://www.pydev.org/manual_101_root.html

If you still can't have it working, please post a screenshot of your PyDev Package Explorer.

Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78
  • I guess I has the necessary directories added to `PYTHONPATH`. I just created new clean project to fiddle with pydev unit test cases. But I am still getting same error. I have added the screenshots to the original question. Can you check once? – MsA Oct 03 '18 at 07:58
  • [...continued] I guess I should be trying out [this](https://stackoverflow.com/a/27605646/6357916). But it answers in the context of linux, which I am not able to get. – MsA Oct 03 '18 at 09:13
  • You should usually not have nested source folders (i.e.: leave only `${PROJECT_DIR_NAME}/Test`) as a source folder and see if it fixes -- if that's what you want -- if you want to have only `${PROJECT_DIR_NAME}` try adding a `__init__.py` file inside the `Test` directory. – Fabio Zadrozny Oct 03 '18 at 15:40
  • I tried by removing `/${PROJECT_DIR_NAME}` from source folder list. Still getting the same behavior. I found post asking for "why PYTHONPATH not found when the containing directory is part of PYTHONPATH and file exists". But the [answer there](https://stackoverflow.com/a/27605646/6357916) seems very linux specific. Can this resolve my issue. Also can this be something related? – MsA Oct 04 '18 at 07:35
  • Ohhh my project was pointing to python installed on cygwin. I changed to project's python interpreter to point to python installed directly on Windows, and it all started working!!! Why is this so? – MsA Oct 04 '18 at 11:16
  • 1
    Oh, that explains things... cygwin sees paths in one way and Eclipse/PyDev itself in another way, and that mix doesn't work well in practice. – Fabio Zadrozny Oct 04 '18 at 12:56
  • But is there absolutely no way to make pyunit test cases work? I have some weird stuff going on here. I am using python logger and its debug logs only appear in log files when I am pointing to cygwin python. And when I switch to windows python, those debugs logs do not appear. And now pyunit test cases work on windows python, but not in cygwin python :\ – MsA Oct 05 '18 at 07:57
  • Unfortunately no, using the python from cygwin is not well supported (if you want to use Linux, then you're expected to do all on Linux). Regarding logging not working, that's probably a different issue (it should definitely work on Python regardless of the platform). Also, if I can suggest something I highly recommend using `pytest` instead of the `unittest` module (note that you have to configure PyDev to use the `pytest` runner in this case: http://www.pydev.org/manual_adv_pyunit.html). – Fabio Zadrozny Oct 05 '18 at 11:11