I need to write unit tests for each validator in my code. As I understand, during the tests my validators return False every time, because email, name, address, etc. attributes are None. Is there a way to separate attributes that are tested (and test each individually) or I have to provide correct email, name, address, etc. if I’m testing phone?
Here is the class in Flask and test code:
class RegistrationForm(FlaskForm): email = StringField('Email', validators=[InputRequired(), Email('Please provide an existing email')]) phone = IntegerField('Phone', validators=[InputRequired(), NumberLength(10, 10, 'Please enter 10-digit number')]) name = StringField('Name', validators=[InputRequired()]) address = StringField('Address', validators=[InputRequired()]) index = IntegerField('Index', validators=[InputRequired()]) comment = StringField() class TestValidators(unittest.TestCase): def setUp(self): self.client = app.test_client() app.config['TESTING'] = True app.config["SECRET_KEY"] = "test_secret" app.config["WTF_CSRF_ENABLED"] = False def test_email_validator(self): valid_phones = [1234567890, 9987654321] invalid_phones = ['qwertyuiop', 123456, None, ''] with app.app_context(): for phone_n in valid_phones: test_form = RegistrationForm(data={'phone': int(phone_n)}) self.assertTrue(test_form.phone.validate(), f'Phone {phone_n} didn't pass the test') for phone_n in invalid_phones: test_form = RegistrationForm(data={'phone': phone_n}) self.assertFalse(test_form.phone.validate(), f'Phone {phone_n} didn't pass the test')
I hope you’ll get my point
submitted by /u/Dzhama_Omarov
[link] [comments]
r/learnpython I need to write unit tests for each validator in my code. As I understand, during the tests my validators return False every time, because email, name, address, etc. attributes are None. Is there a way to separate attributes that are tested (and test each individually) or I have to provide correct email, name, address, etc. if I’m testing phone? Here is the class in Flask and test code: class RegistrationForm(FlaskForm): email = StringField(‘Email’, validators=[InputRequired(), Email(‘Please provide an existing email’)]) phone = IntegerField(‘Phone’, validators=[InputRequired(), NumberLength(10, 10, ‘Please enter 10-digit number’)]) name = StringField(‘Name’, validators=[InputRequired()]) address = StringField(‘Address’, validators=[InputRequired()]) index = IntegerField(‘Index’, validators=[InputRequired()]) comment = StringField() class TestValidators(unittest.TestCase): def setUp(self): self.client = app.test_client() app.config[‘TESTING’] = True app.config[“SECRET_KEY”] = “test_secret” app.config[“WTF_CSRF_ENABLED”] = False def test_email_validator(self): valid_phones = [1234567890, 9987654321] invalid_phones = [‘qwertyuiop’, 123456, None, ”] with app.app_context(): for phone_n in valid_phones: test_form = RegistrationForm(data={‘phone’: int(phone_n)}) self.assertTrue(test_form.phone.validate(), f’Phone {phone_n} didn’t pass the test’) for phone_n in invalid_phones: test_form = RegistrationForm(data={‘phone’: phone_n}) self.assertFalse(test_form.phone.validate(), f’Phone {phone_n} didn’t pass the test’) I hope you’ll get my point submitted by /u/Dzhama_Omarov [link] [comments]
I need to write unit tests for each validator in my code. As I understand, during the tests my validators return False every time, because email, name, address, etc. attributes are None. Is there a way to separate attributes that are tested (and test each individually) or I have to provide correct email, name, address, etc. if I’m testing phone?
Here is the class in Flask and test code:
class RegistrationForm(FlaskForm): email = StringField('Email', validators=[InputRequired(), Email('Please provide an existing email')]) phone = IntegerField('Phone', validators=[InputRequired(), NumberLength(10, 10, 'Please enter 10-digit number')]) name = StringField('Name', validators=[InputRequired()]) address = StringField('Address', validators=[InputRequired()]) index = IntegerField('Index', validators=[InputRequired()]) comment = StringField() class TestValidators(unittest.TestCase): def setUp(self): self.client = app.test_client() app.config['TESTING'] = True app.config["SECRET_KEY"] = "test_secret" app.config["WTF_CSRF_ENABLED"] = False def test_email_validator(self): valid_phones = [1234567890, 9987654321] invalid_phones = ['qwertyuiop', 123456, None, ''] with app.app_context(): for phone_n in valid_phones: test_form = RegistrationForm(data={'phone': int(phone_n)}) self.assertTrue(test_form.phone.validate(), f'Phone {phone_n} didn't pass the test') for phone_n in invalid_phones: test_form = RegistrationForm(data={'phone': phone_n}) self.assertFalse(test_form.phone.validate(), f'Phone {phone_n} didn't pass the test')
I hope you’ll get my point
submitted by /u/Dzhama_Omarov
[link] [comments]