0

I create a web interface for a site that will work with certificates using the Flask framework. I want to learn how to perform unit tests. I created a file that will run the test and validate the routers.py file.

Test.py code:

import unittest
import sys
sys.path.insert(0, 'app/')
import routes


class FlaskrTestCase(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_my_file(self):
        self.assertTrue(Certificate.IsValid(all_cert[2]))


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

I think the error is due to this part of the code in the routers.py:

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html', title='Home', all_cert = all_cert)


@app.route('/all_certificates')
def all_certificates():
    return render_template('all_certificates.html', title='all_certificates', all_cert = all_cert)

This works when I execute the "flask run" command, but if I enter python3 -m unittest test.py I get an error:

  File "/home/marka/Документы/Practice/test.py", line 4, in <module>
    import routes
  File "app/routes.py", line 30, in <module>
    @app.route('/index')
  File "/home/marka/Документы/Practice/venv/lib/python3.5/site-packages/flask/app.py", line 1251, in decorator
    self.add_url_rule(rule, endpoint, f, **options)
  File "/home/marka/Документы/Practice/venv/lib/python3.5/site-packages/flask/app.py", line 67, in wrapper_func
    return f(self, *args, **kwargs)
  File "/home/marka/Документы/Practice/venv/lib/python3.5/site-packages/flask/app.py", line 1222, in add_url_rule
    'existing endpoint function: %s' % endpoint)
AssertionError: View function mapping is overwriting an existing endpoint function: index

I found a similar thread on the forum: AssertionError: View function mapping is overwriting an existing endpoint function: main but I do not know how to apply it in my code. What caused the error? How to use decorators differently?

I tried to rename the function index:

def index():
    index.func_name = func.func_name
    return render_template('index.html', title='Home', all_cert = all_cert)

then I get the error:

  File "/home/marka/Документы/Practice/test.py", line 4, in <module>
    import routes
  File "app/routes.py", line 33
    return render_template('index.html', title='Home', all_cert = all_cert)
                                                                          ^
IndentationError: unindent does not match any outer indentation level

In the test, I would like to check the functions that store the certificate and verify its validity. All my code in https://github.com/Butyrate/CertificationCenter.

Thanks.

Mark
  • 43
  • 8

1 Answers1

0

You are getting that error because your routes module is being loaded twice. As you know, python only loads modules one time. The exception to that rule is when you add the module multiple times into the python path:

sys.path.insert(0, 'app/')

If you remove that line, you won't be redefining your main function multiple times. You can check what I'm telling you by just writing a print in the top level of your routes.py file. You will see how it prints two times.

hernan
  • 572
  • 4
  • 10
  • Thank you very much. I thought that with this line I indicate where to look for the routes module. Do you know how I can import this file without calling it 2 times? – Mark Jun 17 '19 at 18:25
  • try with `from app import routes` – hernan Jun 17 '19 at 18:29
  • Hmm... It doens't work. What works in my case: `from app.routes import *`. Thank you very much. Can you explain how to find and fix such errors? – Mark Jun 17 '19 at 18:47