I am trying to create some (I believe these are Integration not Unit) tests for code that interacts with a database and I am not sure this is the right approach.
I would appreciate any reviews and possible improvements.
Using: VS2010 Unit Testing Framework
This is the function I want to test:
public IDbConnection OpenThirdPartyDatabase(string path, string password)
{
var provider = GetProvider(path, password);
return DBObjectFactory.GetObject().GetConnection(provider.Name, true, true);
}
Functions used by OpenThirdPartyDatabase
private static IDBProvider GetProvider(string path, string password)
{
var providers = DBProviders.GetObject();
var providerExists = providers.Cast<IDBProvider>().Any(dbProvider => dbProvider.Name == "MyProviderName");
return providerExists ? providers.GetDBProvider("MyProviderName") : CreateProvider(path, password);
}
private static IDBProvider CreateProvider(string path, string password)
{
IDBProvider provider = new DBAccessProvider("MyProviderName", path, "", password);
DBProviders.GetObject().Add(provider);
return provider;
}
public IDbConnection GetConnection(string ProviderName, bool OpenConnection, bool UseConnectionPool)
{
IDBProvider dbProvider = DBProviders.GetObject().GetDBProvider(ProviderName);
if (dbProvider == null)
throw new UndeclaredDBProviderException(ProviderName);
else
{
IDbConnection newConnection = null;
if ((connectionPool.ContainsConnection(ProviderName)) && (UseConnectionPool))
newConnection = connectionPool.GetConnection(ProviderName);
else
{
newConnection = dbProvider.GetConnection();
if (UseConnectionPool)
connectionPool.AddConnection(ProviderName, newConnection);
}
if ((OpenConnection) && (newConnection.State == ConnectionState.Closed))
newConnection.Open();
return newConnection;
}
}
Tests:
/// <summary>
/// Test Connection with valid path and password
/// </summary>
[TestMethod]
public void ThirdPartyConnection_Valid()
{
var testConnection = _thirdPartyConnManager.OpenThirdPartyDatabase("Data\\test.mdb", "pass");
Assert.AreNotEqual(testConnection, null);
}
/// <summary>
/// Test Connection With Invalid Password
/// </summary>
[TestMethod]
[ExpectedException(typeof(OleDbException),"Not a valid password.")]
public void ThirdPartyConnection_ShouldThrowOleDbException_InvalidPassword()
{
var testConnection = _thirdPartyConnManager.OpenThirdPartyDatabase("Data\\test.mdb", "invalidpassword");
}
/// <summary>
/// Test Connection with invalid path
/// </summary>
[TestMethod]
[ExpectedException(typeof(OleDbException))]
public void ThirdPartyConnection_ShouldThrowOleDbException_InvalidPath()
{
var testConnection = _thirdPartyConnManager.OpenThirdPartyDatabase("InvalidPath\\Data\\test.mdb", "pass");
}
/// <summary>
/// Test Connection String
/// </summary>
[TestMethod]
public void ThirdPartyConnection_ConnectionString()
{
var testConnection = _thirdPartyConnManager.OpenThirdPartyDatabase("Data\\test.mdb", "pass");
Assert.AreEqual(testConnection.ConnectionString,"Expected Connection String","Not expected Connection String");
}
-
\$\begingroup\$ Integration tests are a scam ... video => infoq.com/presentations/integration-tests-scam text => infoq.com/news/2009/04/jbrains-integration-test-scam \$\endgroup\$Alfred– Alfred2012年04月29日 13:36:07 +00:00Commented Apr 29, 2012 at 13:36
2 Answers 2
I don't claim to be an expert, but here's my experience.
Apart from that i see you are using a constant string "MyProviderName" in your code and you would do well to move that to a const string for easy refactoring and future changes.
Also i suggest you use the more common "ArgumentNullException" vs creating your own custom "UndeclaredDBProviderException" as you want to capture that a null parameter was passed to the function.
I would also suggest you run the code through FxCop and fix any errors.
Your test code looks very reasonable. KRam's comments are also appropriate, but I think you are definitely on the right track.