Back to Devexpress

Test Validation Rules

expressappframework-113294-debugging-testing-and-error-handling-functional-tests-easy-test-write-tests-human-readable-test-validation-rules.md

latest5.1 KB
Original Source

Test Validation Rules

  • Feb 20, 2026
  • 3 minutes to read

The EasyTest functional testing framework allows you to test validation rules.

To see step-by-step testing instructions for beginners, refer to the Test an Action topic.

Suppose you have an Employee business class, which declares the Name and Age properties. The properties have the following requirements:

The following code snippet demonstrates how to apply these validation rules:

csharp
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
using DevExpress.Persistent.Validation;
using System.ComponentModel.DataAnnotations;
// ...
[DefaultClassOptions,ImageName("BO_Employee")]
public class Employee : BaseObject {
    [RuleRequiredField("EmployeeIsAdult", DefaultContexts.Save)]
    public virtual string Name { get; set; }
    [RuleValueComparison("EmployeeNameIsRequired", DefaultContexts.Save, ValueComparisonType.GreaterThanOrEqual, 18)]
    public virtual int Age { get; set; }
}

// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
csharp
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
// ...
[DefaultClassOptions, ImageName("BO_Employee")]
public class Employee : BaseObject {
    public Employee(Session session) : base(session) { }
    private string name;
    [RuleRequiredField("EmployeeIsAdult", DefaultContexts.Save)]
    public string Name {
        get => name;
        set => SetPropertyValue(nameof(Name), ref name, value);
    }
    private int age;
    [RuleValueComparison("EmployeeNameIsRequired", DefaultContexts.Save, ValueComparisonType.GreaterThanOrEqual, 18)]
    public int Age {
        get => age;
        set => SetPropertyValue(nameof(Age), ref age, value);
    }
}

CheckValidationResult Command

In a Windows Forms application, the popup window is displayed when a validation error occurs. In a Blazor application, the validation results are displayed on the current page using the ErrorInfoControl (for more information, refer to the Error Handling in ASP.NET Core Blazor topic). To test all platforms in a single test script, you can use the CheckValidationResult EasyTest command. The image below illustrates the meaning of the command’s parameters on WinForms platform:

To test the Employee object’s validation rules, you can use the following EasyTest script.

ets
;#DropDB MySolutionEasyTest

#Application MySolutionWin
#Application MySolutionWeb
#Application MySolutionBlazor

;Create a new Employee:
*Action Navigation(Employee)
*Action New

;Save it with initial property values (empty Name and zero Age):
*Action Save

;Test the validation result that is displayed when saving:
*CheckValidationResult
 Message = Data Validation Error: *
 Info = "Name" must not be empty.
 Info = "Age" must be greater than or equal to "18".

;Close the error message:
#IfDef MySolutionWin
*Action Close
#EndIf

;Specify a valid Name and invalid Age:
*FillForm
 Name = Mary Tellitson
 Age = 17

;Save an Employee with invalid Age:
*Action Save

;Test the validation result that is displayed when saving:
*CheckValidationResult
 Message = Data Validation Error: *
 Info = "Age" must be greater than or equal to "18".

;Close the error message:
#IfDef MySolutionWin
*Action Close
#EndIf

;Specify a valid Age:
*FillForm
 Age = 18

;Save a valid Employee:
*Action Save

;Check that an error is not displayed:
!CheckValidationResult
 Message = ?*

See Also

Trigger Validation Programmatically and Customize Default Rule Behavior

Test an Action

Test Conditional Appearance Rules