Anand
Anand

Reputation: 3760

Why does coverage run not find flask module, even though unittest can find it?

I have a flask project that works fine when I run unittest, but fails with ModuleNotFoundError: No module named 'flask' when I run coverage.

Relevant code and shell sessions below:

$ python -m unittest
..
----------------------------------------------------------------------
Ran 2 tests in 0.041s

OK

$ coverage run -m unittest
E
======================================================================
ERROR: test.test (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test.test
Traceback (most recent call last):
  File "<...>/.pyenv/versions/3.8.5/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "<...>/.pyenv/versions/3.8.5/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "<path to project>/test/test.py", line 3, in <module>
    from main import app
  File "<...>/main.py", line 1, in <module>
    from flask import Flask, jsonify, request
ModuleNotFoundError: No module named 'flask'
# main.py
from flask import Flask, jsonify, request

app = Flask(__name__)

...
# requirements.txt

...
Flask==1.1.2
...
# test.py

import unittest
from main import app

...

Upvotes: 0

Views: 811

Answers (1)

Anand
Anand

Reputation: 3760

My friends helped me debug this - turned out that even when I had coverage installed in my venv, it was picking up coverage from the global environment.

Using python -m coverage run -m unittest instead picked up the correct coverage, and it then ran perfectly.

Upvotes: 1

Related Questions