0

I am writing unit test for my flask API, when I run:

python -m unittest test

I get :

Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/usr/lib/python2.7/unittest/__main__.py", line 12, in <module>
    main(module=None)
  File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__
    self.parseArgs(argv)
  File "/usr/lib/python2.7/unittest/main.py", line 149, in parseArgs
    self.createTests()
  File "/usr/lib/python2.7/unittest/main.py", line 158, in createTests
    self.module)
  File "/usr/lib/python2.7/unittest/loader.py", line 130, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/lib/python2.7/unittest/loader.py", line 91, in loadTestsFromName
    module = __import__('.'.join(parts_copy))
  File "test.py", line 19, in <module>
    from rosbag_deserializer_api import app
ImportError: cannot import name app

Here is my code:

from flasgger import Swagger
from flask import Flask, jsonify, request
from flask_cors import CORS
import os
import sys
from unittest import TestCase

sys.path.append('../src/rr_rosbag_deserializer')
sys.path.append('../scripts')

from rosbag_deserializer_api import app
from rosbag_deserializer_core import RosbagUploader, RosbagStatus
from utils import ReturnCodes


class TestAPI(TestCase):

    def setUp(self):
         app['TESTING'] = True
         self.app = app.test_client()

    def test_deserializer(self):

        data = {
          "buffer": 5000,
          "es_addr": "http://localhost:8000/",
          "es_index": "change_thiss",
          "es_type": "rosbag",
          "json": "r.json",
          "name": "2018-01-31-15-02-20.bag"
        }
        response = self.app.post('/deserializer', json=data)
        self.assertEquals(response.status_code, 200)



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

Am I running this incorrectly? My app runs fine, I am just trying to write some unit tests. I tried adding __init__.py, however, it did not help. I heard something about circular imports being a problem but I don't know what that means.

Adeel Ahmad
  • 1,033
  • 1
  • 11
  • 24
LoveMeow
  • 3,858
  • 9
  • 44
  • 66
  • 2
    what's the file structure of your project? – Dušan Maďar Feb 13 '18 at 15:10
  • Maybe this answer will be of help, try to organize a package and get rid of path appends. See https://stackoverflow.com/questions/48024321/unit-tests-not-importing-project-modules-python/48024761#48024761 – progmatico Feb 13 '18 at 15:20
  • ...and also https://stackoverflow.com/questions/47499730/a-clean-way-to-organize-and-load-files-with-subclasses/47501913#47501913 – progmatico Feb 13 '18 at 15:26

0 Answers0