Assert.AreEqual Method (Object, Object)

Verifies that two specified objects are equal. The assertion fails if the objects are not equal.

Namespace:  Microsoft.VisualStudio.TestTools.UnitTesting
Assembly:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)

Syntax

'Declaration
Public Shared Sub AreEqual ( _
    expected As Object, _
    actual As Object _
)
public static void AreEqual(
    Object expected,
    Object actual
)
public:
static void AreEqual(
    Object^ expected, 
    Object^ actual
)
static member AreEqual : 
        expected:Object * 
        actual:Object -> unit 
public static function AreEqual(
    expected : Object, 
    actual : Object
)

Parameters

  • expected
    Type: System.Object
    The first object to compare. This is the object the unit test expects.
  • actual
    Type: System.Object
    The second object to compare. This is the object the unit test produced.

Exceptions

Exception Condition
AssertFailedException

expected is not equal to actual.

Remarks

Different numeric types are treated as equal if the logical values are equal. For example, 42L is equal to 42:

Form1 Load method being tested:

public partial class Form1 : Form
    {
        public int a = 42;
        public long b = 42;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            a = 42;
            b = 42;
            
        }
    }

Assert.AreEqual in the unit test:

Assert.AreEqual(target.a, target.b);

This does not apply if you cast different numeric types directly into objects. For example:

Assert.AreEqual((object)target.a, (object)target.b);

-or-

Assert.AreEqual((object)42, (object)42L);

.NET Framework Security

See Also

Reference

Assert Class

AreEqual Overload

Microsoft.VisualStudio.TestTools.UnitTesting Namespace

Other Resources

Using the Assert Classes