Hello.
I want to check with Unit Test , or does my function work correctly and don’t know how to replace an if-else block?

 [TestMethod] public void Test_CreateDatabase() { String database = "test_db"; Program.CreateDatabase(@"C:\Temp\create_database.sql", database); String sqlConnectionString = String.Format("Server = server; Database = {0}; Trusted_Connection = True;", database); String countTables = "SELECT COUNT(*) FROM information_schema.tables "; using (SqlConnection connection = new SqlConnection(sqlConnectionString)) { var server = new Server(new ServerConnection(connection)); var result = server.ConnectionContext.ExecuteScalar(countTables); if (result != 0) { bool temp = true; } else { bool temp = false; } } } 
  • one
    The VS unit tests use the methods Assert.IsNull , Assert.AreEqual , Assert.IsTrue , etc. If I understand correctly what you want, then your Assert.AreNotEqual method. - Mark Shevchenko
  • What exactly is your test checking? What are the tables in the database? Then Assert.IsTrue(result > 0, "Expected to have some tables"); . - andreycha
  • The test checks whether tabs have been added to the database. If tables are added, result =1 . It follows that if the tables are added, the Assert.AreEqual(Convert.ToInt32(result), 1) condition will be true . If the Assert.AreEqual(Convert.ToInt32(result), 1) condition is not met, then Test failed . Do I get it right? - olmar102
  • Yes, right. Just keep in mind that your test does not check the composition of the tables. Those. The script can create any one table. Perhaps it makes sense in the query to return the names of the tables and check not only their number, but also the names. - andreycha

1 answer 1

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:

  1. Assert - class (here you can find other types of Assert )
  2. CollectionAssert - class (assertions associated with collections)
  3. Using Assert Classes
  4. Creating and running unit tests for existing code
  • one
    Instead of Assert.AreEqual(result, 1) it is better to write Assert.AreEqual(1, result) , because the first parameter of the method is called expected , and the second is actual . That is, first there should be a value that should be, and only then - a value that actually turned out. - Mark Shevchenko
  • @MarkShevchenko, yes, you are undoubtedly right. I'll edit now. expected - the value expected by the unit test process - Denis Bubnov