How to run unit tests with tox in a newly generated Tryton module?

I created a new module using the Tryton cookiecutter. When I try to run tox -e py39sqlite, I get this error:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\path\\to\\module\\sample\\.tox\\py39-sqlite\\lib\\site-packages\\trytond\\modules\\sample\\tryton.cfg'

Did I miss a step in setting up the module or running tox?

The name of the environment is py39-sqlite

Yes, I mistakenly posted py39sqlite instead of py39-sqlite, but I still couldn’t resolve my error

We need to full traceback of the error in order to understand anything.

This is the full traceback, I’m using a virtual environment to run the test command python -m unittest,

Removed Image

You need to run tox to run tests with tox.

I initially did run tox, here is my output,

PS C:\Users\amrit\Documents\Continental\Projects\Tryton\docker\sample_production> tox -e py39-sqlite
.pkg: _optional_hooks> python c:\users\amrit\documents\continental\projects\tryton\main\venv\lib\site-packages\pyproject_api\_backend.py True setuptools.build_meta __legacy__
.pkg: get_requires_for_build_editable> python c:\users\amrit\documents\continental\projects\tryton\main\venv\lib\site-packages\pyproject_api\_backend.py True setuptools.build_meta __legacy__
.pkg: build_editable> python c:\users\amrit\documents\continental\projects\tryton\main\venv\lib\site-packages\pyproject_api\_backend.py True setuptools.build_meta __legacy__
py39-sqlite: install_package> python -I -m pip install --force-reinstall --no-deps C:\Users\amrit\Documents\Continental\Projects\Tryton\docker\sample_production\.tox\.tmp\package\5\trytond_sample_production-7.4.0-0.editable-py3-none-any.whl
py39-sqlite: commands[0]> coverage run --omit=*/tests/* -m xmlrunner discover -s tests
C:\Users\amrit\Documents\Continental\Projects\Tryton\docker\sample_production\.tox\py39-sqlite\lib\site-packages\trytond\__init__.py:27: UserWarning: Timezone must be set to UTC instead of India Standard Time
  warnings.warn('Timezone must be set to UTC instead of %s' % time.tzname[0])

Running tests...
----------------------------------------------------------------------
.....................E
======================================================================
ERROR [0.004s]: test_xml_files (test_module.SampleProductionTestCase)
Test validity of the xml files of the module
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\amrit\Documents\Continental\Projects\Tryton\docker\sample_production\.tox\py39-sqlite\lib\site-packages\trytond\tests\test_tryton.py", line 281, in wrapper
    result = func(*args, **kwargs)
  File "C:\Users\amrit\Documents\Continental\Projects\Tryton\docker\sample_production\.tox\py39-sqlite\lib\site-packages\trytond\tests\test_tryton.py", line 1076, in test_xml_files
    with file_open('%s/tryton.cfg' % self.module,
  File "C:\Users\amrit\Documents\Continental\Projects\Tryton\docker\sample_production\.tox\py39-sqlite\lib\site-packages\trytond\tools\misc.py", line 50, in file_open
    return io.open(path, mode, encoding=encoding)
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\amrit\\Documents\\Continental\\Projects\\Tryton\\docker\\sample_production\\.tox\\py39-sqlite\\lib\\site-packages\\trytond\\modules\\sample_production\\tryton.cfg'

----------------------------------------------------------------------
Ran 22 tests in 11.968s

FAILED (errors=1)

Generating XML reports...
py39-sqlite: exit 1 (25.05 seconds) C:\Users\amrit\Documents\Continental\Projects\Tryton\docker\sample_production> coverage run --omit=*/tests/* -m xmlrunner discover -s tests pid=28232
py39-sqlite: commands_post[0]> coverage report
Name          Stmts   Miss  Cover
---------------------------------
__init__.py       6      0   100%
---------------------------------
TOTAL             6      0   100%
py39-sqlite: commands_post[1]> coverage xml
Wrote XML report to coverage.xml
  py39-sqlite: FAIL code 1 (27.70=setup[2.20]+cmd[25.05,0.22,0.24] seconds)
  evaluation failed :( (28.66 seconds)

I suspect that your module is not importable.
Can you run it inside your virtualenv?

this was already run in the virtual environment

$ which python
/c/Users/amrit/Documents/Continental/Projects/Tryton/main/venv/Scripts/python
$ tox -e py39-sqlite
.pkg: _optional_hooks> python c:\users\amrit\documents\continental\projects\tryton\main\venv\lib\site-packages\pyproject_api\_backend.py True setuptools.build_meta __legacy__
.pkg: get_requires_for_build_editable> python c:\users\amrit\documents\continental\projects\tryton\main\venv\lib\site-packages\pyproject_api\_backend.py True setuptools.build_meta __legacy__
.pkg: build_editable> python c:\users\amrit\documents\continental\projects\tryton\main\venv\lib\site-packages\pyproject_api\_backend.py True setuptools.build_meta __legacy__
py39-sqlite: install_package> python -I -m pip install --force-reinstall --no-deps C:\Users\amrit\Documents\Continental\Projects\Tryton\docker\sample_production\.tox\.tmp\package\6\trytond_sample_production-7.4.0-0.editable-py3-none-any.whlpy3-none-any.whl
py39-sqlite: commands[0]> coverage run --omit=*/tests/* -m xmlrunner discover -s tests
C:\Users\amrit\Documents\Continental\Projects\Tryton\docker\sample_production\.tox\py39-sqlite\lib\site-packages\trytond\__init__.py:27: UserWarning: Timezone must be set to UTC instead of India Standard Time
  warnings.warn('Timezone must be set to UTC instead of %s' % time.tzname[0])

Running tests...
----------------------------------------------------------------------
.....................E
======================================================================
ERROR [0.003s]: test_xml_files (test_module.SampleProductionTestCase)
Test validity of the xml files of the module
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\amrit\Documents\Continental\Projects\Tryton\docker\sample_production\.tox\py39-sqlite\lib\site-packages\trytond\tests\test_tryton.py", line 281, in wrapper
    result = func(*args, **kwargs)
  File "C:\Users\amrit\Documents\Continental\Projects\Tryton\docker\sample_production\.tox\py39-sqlite\lib\site-packages\trytond\tests\test_tryton.py", line 1076, in test_xml_files
    with file_open('%s/tryton.cfg' % self.module,
  File "C:\Users\amrit\Documents\Continental\Projects\Tryton\docker\sample_production\.tox\py39-sqlite\lib\site-packages\trytond\tools\misc.py", line 50, in file_open
    return io.open(path, mode, encoding=encoding)
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\amrit\\Documents\\Continental\\Projects\\Tryton\\docker\\sample_production\\.tox\\py39-sqlite\\lib\\site-packages\\trytond\\modules\\sample_production\\tryton.cfg'

----------------------------------------------------------------------
Ran 22 tests in 11.703s

FAILED (errors=1)

Generating XML reports...
py39-sqlite: exit 1 (24.66 seconds) C:\Users\amrit\Documents\Continental\Projects\Tryton\docker\sample_production> coverage run --omit=*/tests/* -m xmlrunner discover -s tests pid=33628
py39-sqlite: commands_post[0]> coverage report
Name          Stmts   Miss  Cover
---------------------------------
__init__.py       6      0   100%
---------------------------------
TOTAL             6      0   100%
py39-sqlite: commands_post[1]> coverage xml
Wrote XML report to coverage.xml
  py39-sqlite: FAIL code 1 (27.52=setup[2.47]+cmd[24.66,0.20,0.19] seconds)
  evaluation failed :( (28.50 seconds)

I mean running trytond server with your module (not the tests).
Because I suspect that your module is not importable.

Do you mean this ?

import trytond.modules.party
import trytond.modules.sample_production

This is in my virtual environment
I added party, because it’s a standard module, I have it installed,

$ python script.py 
C:\Users\amrit\Documents\Continental\Projects\Tryton\main\venv\lib\site-packages\trytond\__init__.py:27: UserWarning: Timezone must be set to UTC instead of India Standard Time
  warnings.warn('Timezone must be set to UTC instead of %s' % time.tzname[0])
Traceback (most recent call last):
  File "C:\XXXXX\migration\script.py", line 2, in <module>
    import trytond.modules.sample_production
ModuleNotFoundError: No module named 'trytond.modules.sample_production'

Btw, I did do pip install . inside the folder I’m using to develop my module, after that, tests were working, but I was wondering if I have to pip install my development module, and check if it pass all the tests, I read somewhere I can use editable mode like pip install -e ., but I did that also, still getting the same traceback.

Hi, did you find anything wrong with this?

So if after installing the custom module in editable mode, it is still not importable. This explain why the tests are not working. You must first fix your module to be importable.

I can not help on what is wrong in your module because you do not provide any traceback about its failure to be imported.

I provided here the details u asked me and imported my modules, the thing is the module whatever I’m trying to import should be inside the trytond/modules/ folder right? but I don’t see my custom module in there, but the party module, that’s why I said I installed in editable mode, it is still not importable. I can run the commands again, if that will help narrow down the problem, ModuleNotFoundError: No module named 'trytond.modules.sample_production', this was the traceback, I mean it just means, by custom module folder did not go into the trytond folder, in my virtual environment. If u can specifically tell me, what python code to run, I can give the full traceback.

This might be an error in you module. trytond’s custom import mechanisms has some shortcomings, one of which is that it “converts” ImportErrors into “FileNotFoundError: …/trytond.conf”. I suggest adding a raise statement just behind the except ModuleNotFoundError in trytond.tools.misc.find_file().

When you install in editable mode, depending on the version setuptools, you must use the option --use-pep517.

1 Like