Assert - class - tests conditions in unit tests using the statement "True / False." This class contains a set of static methods that evaluate a logical condition. If this condition is true
, it passes statements. The statement tests the assumption of truth for the conditions being compared. If the condition being checked is not fulfilled, the assertion is considered false.
Consider a few examples of tests, based on your code in question with a few explanations and digressions.
Test using the Assert.AreEqual method (The method checks that the specified values are equal). The test will be considered completed successfully if the result
is 0
:
[TestMethod] public void TestMethodAssertAreEqual() { var result = 0; // предположим, что мы получили это значение Assert.AreEqual(0, result); }
Test using the Assert.AreNotEqual method (The method checks that the specified values are not equal). The test will be considered completed successfully, if result
not equal to 1
, for example 0
- successful completion of the test:
[TestMethod] public void TestMethodAssertAreNotEqual() { var result = 0; // предположим, что мы получили это значение Assert.AreNotEqual(1, result); }
Consider also the situation when, during the execution of a method, an exception is thrown that is expected. For example, a parameter was passed to the method that throws an exception. For this we will help the attribute ExpectedExceptionAttribute . Code example:
[TestMethod] [ExpectedException(typeof(Exception))] public void TestExpectedException() { var result = server.ConnectionContext.ExecuteScalar(0); // передадим плохой параметр }
Just assume that if server.ConnectionContext.ExecuteScalar
pass 0
to the server.ConnectionContext.ExecuteScalar
method, an exception should be thrown if the test has the ExpectedExceptionAttribute attribute, then the test will work successfully.
Useful links to explore:
- Assert - class (here you can find other types of
Assert
) - CollectionAssert - class (assertions associated with collections)
- Using Assert Classes
- Creating and running unit tests for existing code
Assert.IsNull
,Assert.AreEqual
,Assert.IsTrue
, etc. If I understand correctly what you want, then yourAssert.AreNotEqual
method. - Mark ShevchenkoAssert.IsTrue(result > 0, "Expected to have some tables");
. - andreycharesult =1
. It follows that if the tables are added, theAssert.AreEqual(Convert.ToInt32(result), 1)
condition will betrue
. If theAssert.AreEqual(Convert.ToInt32(result), 1)
condition is not met, thenTest failed
. Do I get it right? - olmar102