diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..c798475 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,20 @@ +pipeline { + agent { docker { image 'python:3.7.2' } } + stages { + stage('build') { + steps { + sh 'pip install -r requirements.txt' + } + } + stage('test') { + steps { + sh 'python test_app.py' + } + post { + always { + junit 'test-reports/*.xml' + } + } + } + } +} diff --git a/app.py b/app.py new file mode 100644 index 0000000..1a76ed8 --- /dev/null +++ b/app.py @@ -0,0 +1,14 @@ +from flask import Flask +app = Flask(__name__) + +@app.route('/') +@app.route('/hello/') +def hello_world(): + return 'Hello World!\n' + +@app.route('/hello/') # dynamic route +def hello_user(username): + return 'Why Hello %s!\n' % username + +if __name__ == '__main__': + app.run(host='0.0.0.0') # open for everyone \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..41df704 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +Click==7.0 +Flask==1.0.2 +itsdangerous==1.1.0 +Jinja2==2.10 +MarkupSafe==1.1.0 +Werkzeug==0.14.1 +xmlrunner==1.7.7 +# pytest==6.1.2 +# pytest-cov==2.10.1 diff --git a/test_app.py b/test_app.py new file mode 100644 index 0000000..e7460cb --- /dev/null +++ b/test_app.py @@ -0,0 +1,31 @@ +import unittest +import app + +class TestHello(unittest.TestCase): + + def setUp(self): + app.app.testing = True + self.app = app.app.test_client() + + def test_hello(self): + rv = self.app.get('/') + self.assertEqual(rv.status, '200 OK') + self.assertEqual(rv.data, b'Hello World!\n') + + def test_hello_hello(self): + rv = self.app.get('/hello/') + self.assertEqual(rv.status, '200 OK') + self.assertEqual(rv.data, b'Hello World!\n') + + def test_hello_name(self): + name = 'Simon' + rv = self.app.get(f'/hello/{name}') + self.assertEqual(rv.status, '200 OK') + self.assertIn(bytearray(f"{name}", 'utf-8'), rv.data) + +if __name__ == '__main__': + ############# Add these lines ############# + import xmlrunner + runner = xmlrunner.XMLTestRunner(output='test-reports') + unittest.main(testRunner=runner) + ###########################################