Skip to content

Commit

Permalink
added flask app and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
shrutikaponde committed Nov 2, 2020
1 parent 8bb15fc commit 6c7229c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -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'
}
}
}
}
}
14 changes: 14 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -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/<username>') # 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
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions test_app.py
Original file line number Diff line number Diff line change
@@ -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)
###########################################

0 comments on commit 6c7229c

Please sign in to comment.