In my case it was Resharper Unit Tests runner (plus NUnit tests, never had such problem with MsTests). After killing the process, was able to rebuild process, without restarting OS or VS2013.
Other test runners, like xUnit can cause the same issue.
What helps then is to check if you can add a Dispose pattern, for example if you're adding a DbFixture and the database contacts isn't disposed properly. That will cause the assembly files being locked even if the tests completed.
Note that you can just add IDisposable interface to your DbFixture and let IntelliSense add the Dispose pattern. Then, dispose the related contained propertys and explicitly assign them to null.
That will help to end the tests in a clean way and unlock related locked files as soon as the tests ended.
Example (DBFixture is used by xUnit tests):
public class DbFixture: IDisposable
{
private bool disposedValue;
public ServiceProvider ServiceProvider { get; private set; }
public DbFixture()
{
// initializes ServiceProvider
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// dispose managed state (managed objects)
ServiceProvider.Dispose();
ServiceProvider = null;
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~DbFixture()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
The same pattern you need for the test class itself - it needs its own Dispose method (as shown for the DbFixture class above):
public SQL_Tests(ITestOutputHelper output)
{
this.Output = output;
var fixture = new DbFixture(); // NOTE: MS Dependency injection framework didn't initialize when the fixture was a constructor param, hence it is here
_serviceProvider = fixture.ServiceProvider;
} // method
So it needs to dispose its local property _serviceProvider
in its own Dispose method, because the test class constructor SQL_Tests
instanciated it.