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.