Only function definitions covered by unit testing python/flask backend -
i start saying beginner @ both flask , unit testing in python, bare in mind.
i writing unit testing backend building android app. trying write tests functions communicate android client when write them, definitions of each function i'm testing registered covered.
my registration function example, looks this:
@app.route("/register", methods=['post']) def register(): register_args = request.get_json(force=true) if not ('username' in register_args , 'email' in register_args , 'password' in register_args): abort(406) new_username = register_args['username'] new_email = register_args['email'] new_password = register_args['password'] if user.query.filter_by(username=new_username).all() or user.query.filter_by(email=new_email).all(): abort(406) else: u = user(username=new_username, email=new_email, password=new_password) db.session.add(u) db.session.commit() return jsonify(u.as_dict())
and test looks this:
def test_registration(self): registration_url = "http://127.0.0.1:5000/register" #password missing data = json.dumps({'username': 'danilzorz', 'email': 'danilzorz@awesome.com'}) response = requests.post(registration_url, data) print("missing password http-code: " + str(response.status_code)) #correct registration data = json.dumps({'username': 'danilzorz', 'email': 'danilzorz@awesome.com', 'password': 'hejhej'}) response = requests.post(registration_url, data) print("correct registration http-code: " + str(response.status_code)) self.asserttrue(response.status_code == 200) #username taken data = json.dumps({'username': 'danilzorz', 'email': 'dude@dude.com', 'password': 'hejhej'}) response = requests.post(registration_url, data) print("incorrect registration http-code: " + str(response.status_code) + "\n") self.asserttrue(response.status_code == 406)
but rows containing app route , function definition tested according both pycharms built in coverage tester , nosetests.
so question is, should coverage rest of function?
any appreciated.
edit: using python 3.4
you should able create request context , call method directly
def test_register(): app = flask(__name__) ctx = app.test_request_context(path="/register?get_arg1=55",method="post",data={"username":"frank","email":"f@ra.nk","password":"password"}) ctx.push() assert register() == expected_register_output
another alternative pull business logic out of view function
def do_register(username,email,password): # // actual registration here , return new user object @app.route("/register",...) def register(): username,password,email = request.form["username"],... if do_register(username,email,password): return registration_success return registration_errors
then call do_register
on test ...
an yet alternative provide comments tell pycov ignore method
@app.route("/register",...) def register(): # pragma: no cover ....
see http://nedbatchelder.com/code/coverage/excluding.html more info on excluding things coverage
Comments
Post a Comment