0

Hi I am working on a python project, and create the python project using pybuilder. The structure is like this:


    | --src
         | -- _init__.py
         | -- main 
                | -- __init__.py
                | -- python
                       | -- __init.py__
                       | -- amass
                              | -- amass.py
     
         | -- unittest
                | -- __init__.py
                | -- python
                       | -- __init__.py
                       | -- my_tests.py

in amass.py I have my main functionality and in the test my_tests.py, I just want to test simply: here , my_tests.py:


    import unittest
    from src.main.python.amass import amass
    
    class MyTestCase(unittest.TestCase):
    
        def test_something(self):
            self.assertEqual(True, True)
    
    
        def test_subdomains_finding_amass(self):
            self.assertTrue(amass.find_subdomains()) # main func in amass.py
    
    
    if __name__ == '__main__':
        unittest.main()

but when I run pyb -v, it has error:

```
[INFO]  Processing dependency packages 'edgegrid-python==1.2.0' to be installed with {}
[INFO]  Processing dependency packages 'mock==4.0.1' to be installed with {}
[INFO]  Processing dependency packages 'moto==1.3.14' to be installed with {}
[INFO]  Processing dependency packages 'pandas==1.3.3' to be installed with {}
[INFO]  Processing dependency packages 'requests==2.20.1' to be installed with {}
[INFO]  Requested coverage for tasks: pybuilder.plugins.python.unittest_plugin:run_unit_tests
[INFO]  Running unit tests
[INFO]  Executing unit tests from Python modules in /Users/hbu/Work/Project-Beacon/src/unittest/python
[INFO]  Executed 1 unit tests
[ERROR] Test has error: unittest.loader._FailedTest.rest_tests
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.7/3.7.10_3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/local/Cellar/python@3.7/3.7.10_3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 628, in run
    testMethod()
  File "/usr/local/Cellar/python@3.7/3.7.10_3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 34, in testFailure
    raise self._exception
ImportError: Failed to import test module: rest_tests
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.7/3.7.10_3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "/Users/xxx/Work/Project-Beacon/src/unittest/python/my_tests.py", line 2, in <module>
    from src.main.python.amass import amass
ModuleNotFoundError: No module named 'src'


------------------------------------------------------------
BUILD FAILED - There were 1 error(s) and 0 failure(s) in unit tests (pybuilder/plugins/python/unittest_plugin.py)
------------------------------------------------------------
```

here is my build.py:

```
from pybuilder.core import init, use_plugin

version = "0.0.1"

use_plugin("python.core")
use_plugin("python.install_dependencies")
use_plugin("python.unittest")
use_plugin("python.coverage")
use_plugin("python.flake8")
use_plugin('python.pycharm')
use_plugin('pypi:pybuilder_aws_plugin')

default_task = ["clean", "analyze", "publish", "package_lambda_code"]


@init
def initialize(project):
    project.name = "finddomain-" + version
    project.depends_on('pandas', version="==1.3.3")
    project.depends_on('edgegrid-python', version="==1.2.0")
    project.depends_on('mock', version="==4.0.1")
    project.depends_on('moto', version="==1.3.14")
    project.depends_on('requests', version="==2.20.1")
    project.build_depends_on("flake8")

    project.set_property('coverage_threshold_warn', 90)
    project.set_property('coverage_break_build', False)
    project.set_property('dir_source_unittest_python', 'src/unittest/python')

    project.set_property('flake8_break_build', False)
    project.set_property('flake8_max_line_length', 200)
    project.set_property('flake8_include_test_sources', True)

```

I tried

python -m unittest rest_tests.MyTestCase.test_subdomains_finding_amass


ImportError: Failed to import test module: rest_tests

Traceback (most recent call last):
  File 
"/usr/local/opt/python@3.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
ModuleNotFoundError: No module named 'rest_tests'

Ran 1 test in 0.000s

FAILED (errors=1)

Anyone can help? Is it because intellij configure as I can click the button to run : enter image description here

Hongli Bu
  • 461
  • 12
  • 37
  • Have you tried to remove the `src` in front of the import in `my_tests.py`? – marcel h Sep 26 '21 at 11:08
  • @marcelh I tried remove ```src``` and ```src/main```, intellij show error and I tried to try pyb -v , get like ```ModuleNotFoundError: No module named 'main'``` – Hongli Bu Sep 26 '21 at 11:49
  • It looks like you need to do `from amass import amass` to make pybuilder work. To make intellij also work with this, you can install the `amass` package into your virtualenv, preferably in editable mode, so that you don't have to reinstall the package every time you change the code. This video is relevant (although doesn't mention pybuilder): https://youtu.be/DhUpxWjOhME – Oli Sep 27 '21 at 01:54
  • @Oli still no luck, get a different error ```BUILD FAILED - TypeError: 'NoneType' object is not callable (xmlrunner/runner.py:66) ``` – Hongli Bu Sep 27 '21 at 02:33
  • What version of pybuilder are you using? Some googling revealed that a bug that looks very similar to this was fixed in version `0.12.7`. – Oli Sep 27 '21 at 13:04

1 Answers1

0

I know this question was posted over a year ago but I just found myself in the same situation: I created a new project using pybuilder, created an initial class or two with associated unit tests, successfully ran pyb clean run_unit_tests and then imported it into IntelliJ Ultimate.

For a fresh, newly imported project, the intellisense in the IDE will suggest amending the module import to be of the form src/path/to/your/module. If you accept the IDE change then you will have a testable run configuration in IntelliJ but pybuilder will give you the error type shown above in the original question.

The solution is to run pyb clean install via the console to ensure your module is installed into your venv. Once installed, the IDE should pick up the module reference in the import in your unit test class.

For completeness, I just quickly created a sample project using pybuilder and tested the below before opening in IntelliJ:

| --src
     | -- _init__.py
     | -- main 
            | -- __init__.py
            | -- python
                   | -- __init.py__
                   | -- hello_world.py
 
     | -- unittest
            | -- __init__.py
            | -- python
                   | -- __init__.py
                   | -- hello_world_tests.py

With a hello_world.py as per:

class hello_world:

    def __init__(self, addressee):
        self.addressee = addressee

    def greeting(self):
        return 'Hello ' + self.addressee

... and hello_world_tests.py:

import unittest

from hello_world import hello_world


class MyTestCase(unittest.TestCase):
    def test_something(self):

        my_object = hello_world('Bob')

        self.assertEqual(my_object.greeting(), "Hello Bob")

if __name__ == '__main__':
    unittest.main()
    

Then, after running pyb clean install via the console, the test class is runnable in the IDE. Hope this helps someone else in the future.

fripster
  • 11
  • 4