I have been reading a lot about unit tests, integration tests, and E2E tests. I understand the premises of each, but I struggle to apply them in practical cases to figure out what to test in my code (tests that are relevant and provide real certainty). What are the indispensable things to test? This has led me to think that tests are somewhat useless (although it’s likely that I am not fully understanding things). I have a simple class in C# with a single method to perform an SQL query. Could you give me an example of a meaningful test that could be applied to it?
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
}
public static class DbHandler
{
public static List<Employee> ExecuteQuery()
{
string connectionString = "Data Source=.;Initial Catalog=bd_prueba;Integrated Security=True;Encrypt=False";
string query = "SELECT * FROM Employees WHERE MiddleName = 'Gasca'";
var employees = new List<Employee>();
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var employee = new Employee
{
Id = Convert.ToInt32(reader["Id"]),
FirstName = reader["FirstName"].ToString(),
LastName = reader["LastName"].ToString(),
MiddleName = reader["MiddleName"].ToString()
};
employees.Add(employee);
}
}
}
}
return employees;
}
}
I read that things like the following could be applied:
TestInvalidConnectionStringHandling
TestConnectionAndQueryWithoutResults
But I consider these tests useless because, although they could be written simply, why test if an error occurs when a connection string is invalid? We all know what it will produce.
Anyway, what I am looking for is to understand and know what is an indispensable test for code like this. How do I know what tests to conduct on a class? Is it normal to feel that many existing tests do not provide real value (beyond repeatability)?
I know there’s a missing try and catch there, but I also want to ask, can tests help identify, for example, this lack of error handlers? And if so, can you show an example?
can tests help identify this lack of error handlers?
What are the indispensable things to test?
Could you give me an example of a meaningful test that could be applied to it?
are the test useless?