Should validation rules be tested?

If I have a method taking input and giving output, it’s a no-brainer that tests should be written. But what about things like validation rules?

For example, I add a validation rule that a certain property must be at least 5 characters long. Should I then write a UI test to check for an error message when the form field is empty? Assume that the validation library itself works fine, the only thing I do is add a one-liner to specify this rule.

I’m leaning towards no, because I’d only be duplicating the validation rules, and if the rules itself are set up incorrectly, the test will probably also be incorrect. However, I’d very much like to hear people’s opinion and experience on this.

Absolutely, including if the underlying library (business layer) works fine.

The reason is that there may be a lot of things going wrong between the business layer and the UI when it comes to validation. In the first case, you can simply throw an exception. In the second case, you have to process the exception, react to it (for example by cancelling the processing or doing or not doing a redirection, displaying a localized message, etc.)

Imagine the validation of an e-mail address. The business layer detected that “someone.somewhere.com” is not a valid e-mail address. The expected behavior is to:

  1. Stop processing,
  2. Stop redirection,
  3. Show a message to the user,
  4. Highlight the e-mail field in red.

What if:

  • The processing is not stopped?
  • The invalid e-mail is simply replaced by a null string?
  • The redirection is still done?
  • The message is not shown?
  • The message is wrong?
  • Another error message for an other field is shown together with the expected message?
  • The message is correct is some cultures, but not in others?
  • The message is too long in some cultures for the area?
  • The e-mail field is not highlighted?
  • Another field is highlighted together with the e-mail field?
  • The e-mail field is empty, i.e. the previous input is discarded?
  • All previous input of the form is discarded?

3

If you don’t write a test to verify that the validation rule is actually functioning how to do know that it worked?

There are customer requirements / user stories that state for the input to be valid it must by 5 characters or longer, have no double letters and no more than 2 numbers. If the user doesn’t meet these standard you warn them, reject the answer, or quit the program; depending on the requirements. So of course you should test these conditions.

3

It’ll only take a few lines of code to test it, and that will 1 – give you confidence that the spec is still being met if you change the code, and 2 – make the tests a better documentation of the spec. Remember that unit tests are documentation and regression tests, not just proof that your first implementation works.

YES you absolutely should test your validation. Then you should test that your UI responds to it correctly as a separate test.

Also if you use regular expressions then a great tool to help you catch unexpected validation errors (or false positives) is Rex as this is quite good at exercising all the paths.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Should validation rules be tested?

If I have a method taking input and giving output, it’s a no-brainer that tests should be written. But what about things like validation rules?

For example, I add a validation rule that a certain property must be at least 5 characters long. Should I then write a UI test to check for an error message when the form field is empty? Assume that the validation library itself works fine, the only thing I do is add a one-liner to specify this rule.

I’m leaning towards no, because I’d only be duplicating the validation rules, and if the rules itself are set up incorrectly, the test will probably also be incorrect. However, I’d very much like to hear people’s opinion and experience on this.

Absolutely, including if the underlying library (business layer) works fine.

The reason is that there may be a lot of things going wrong between the business layer and the UI when it comes to validation. In the first case, you can simply throw an exception. In the second case, you have to process the exception, react to it (for example by cancelling the processing or doing or not doing a redirection, displaying a localized message, etc.)

Imagine the validation of an e-mail address. The business layer detected that “someone.somewhere.com” is not a valid e-mail address. The expected behavior is to:

  1. Stop processing,
  2. Stop redirection,
  3. Show a message to the user,
  4. Highlight the e-mail field in red.

What if:

  • The processing is not stopped?
  • The invalid e-mail is simply replaced by a null string?
  • The redirection is still done?
  • The message is not shown?
  • The message is wrong?
  • Another error message for an other field is shown together with the expected message?
  • The message is correct is some cultures, but not in others?
  • The message is too long in some cultures for the area?
  • The e-mail field is not highlighted?
  • Another field is highlighted together with the e-mail field?
  • The e-mail field is empty, i.e. the previous input is discarded?
  • All previous input of the form is discarded?

3

If you don’t write a test to verify that the validation rule is actually functioning how to do know that it worked?

There are customer requirements / user stories that state for the input to be valid it must by 5 characters or longer, have no double letters and no more than 2 numbers. If the user doesn’t meet these standard you warn them, reject the answer, or quit the program; depending on the requirements. So of course you should test these conditions.

3

It’ll only take a few lines of code to test it, and that will 1 – give you confidence that the spec is still being met if you change the code, and 2 – make the tests a better documentation of the spec. Remember that unit tests are documentation and regression tests, not just proof that your first implementation works.

YES you absolutely should test your validation. Then you should test that your UI responds to it correctly as a separate test.

Also if you use regular expressions then a great tool to help you catch unexpected validation errors (or false positives) is Rex as this is quite good at exercising all the paths.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật