Python unit test Can anyone help me to add 2 unit test in the test web file for
ID: 3734671 • Letter: P
Question
Python unit test
Can anyone help me to add 2 unit test in the test web file for the any classes (search, editor..)in the form file? i appreicate it.
form py file:
"""
Forms
~~~~~
"""
from flask_wtf import Form
from wtforms import BooleanField
from wtforms import TextField
from wtforms import TextAreaField
from wtforms import PasswordField
from wtforms.validators import InputRequired
from wtforms.validators import ValidationError
from wiki.core import clean_url
from wiki.web import current_wiki
from wiki.web import current_users
class URLForm(Form):
url = TextField('', [InputRequired()])
def validate_url(form, field):
if current_wiki.exists(field.data):
raise ValidationError('The URL "%s" exists already.' % field.data)
def clean_url(self, url):
return clean_url(url)
class SearchForm(Form):
term = TextField('', [InputRequired()])
ignore_case = BooleanField(
description='Ignore Case',
# FIXME: default is not correctly populated
default=True)
class EditorForm(Form):
title = TextField('', [InputRequired()])
body = TextAreaField('', [InputRequired()])
tags = TextField('')
class LoginForm(Form):
name = TextField('', [InputRequired()])
password = PasswordField('', [InputRequired()])
def validate_name(form, field):
user = current_users.get_user(field.data)
if not user:
raise ValidationError('This username does not exist.')
def validate_password(form, field):
user = current_users.get_user(form.name.data)
if not user:
return
if not user.check_password(field.data):
raise ValidationError('Username and password do not match.')
Test_file:
from . import WikiBaseTestCase
class WebContentTestCase(WikiBaseTestCase):
"""
Various test cases around web content.
"""
def test_index_missing(self):
"""
Assert the wiki will correctly play the content missing
index page, if no index page exists.
"""
rsp = self.app.get('/')
assert b"You did not create any content yet." in rsp.data
assert rsp.status_code == 200
Explanation / Answer
ANSWER:
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if _name_ == '__main__':
unittest.main()
--------------------------------------------------------------------------------------------------------------------------------------------
Second Way to test unit test case logig below:
import unittest
def fun(x):
return x + 1
class MyTest(unittest.TestCase):
def test(self):
self.assertEqual(fun(3), 4)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.