Most likely, this is due to the fact that LocalDb is not initialized on the VSO build server, which is twisted to start the build.
According to https://github.com/ritterim/automation-sql , LocalDb can be installed but not initialized.
LocalDB installed but cannot connect
You may have installed LocalDB, but you never initialized the instance on your machine. Run this command using the command line.
LocalDB SQL EXPRESS 2014
"C: \ Program Files \ Microsoft SQL Server \ 120 \ Tools \ Binn \ SqlLocalDB.exe" create "v12.0" 12.0 -s LocalDB SQL Express 2012
"C: \ Program Files \ Microsoft SQL Server \ 110 \ Tools \ Binn \ SqlLocalDB.exe" create "v11.0" 11.0 -s Verify that the command worked using SQL Server Management Studio to connect to the instance.
My decision:
Each time VSO starts the build, it creates a new virtual machine from the template to start the build. In my case, I would like to create a small LocalDb database dynamically to run unit tests against.
I am using the RimDev.Automation.Sql nuget package ( http://www.nuget.org/packages/RimDev.Automation.Sql/ ) so that my tests can programmatically create a database.
I also wanted to integrate with Unity and Entity Framework 6 so that it would maximize my access to the database in production. To do this, I created a class on which all my tests are inherited. One thing I wanted was to use the same database for all my tests, so I created a static LocalDb creation.
In addition, initialization runs a set of scripts in the Resources folder to pre-populate the database with stock data, if you wish. You need to make sure that sql scripts are marked for copying to the output folder (see Properties) so that the files are in the correct path when the unit test is executed.
If you want to create a new database for each test class or even for each test, this can be easily changed.
using System; using System.Data.Entity; using System.IO; using Microsoft.Practices.Unity; using Playground.Model; using RimDev.Automation.Sql; namespace Playground.UnitTests { public abstract class TestBaseClass {
Here is a test example:
using System.Linq; using Microsoft.Practices.Unity; using Microsoft.VisualStudio.TestTools.UnitTesting; using Playground.Model; namespace Playground.UnitTests { [TestClass] public class UnitTest1 : TestBaseClass { [Dependency] public PlaygroundEntities Db { get; set; } private static bool _initialized = false; [TestInitialize] public void TestInitialize() { if (!_initialized) { Db.Playgrounds.Add(new Playground.Model.Playground() {Name = "Dave playground", Location = "SomeTown"}); Db.SaveChanges(); _initialized = true; } } [TestMethod] public void TestMethod1() { var p = Db.Playgrounds.FirstOrDefault(pg => pg.Name == "Dave playground"); Assert.IsNotNull(p); } [TestMethod] public void TestMethod2() { var p = Db.Playgrounds.FirstOrDefault(pg => pg.Location == "SomeTown"); Assert.IsNotNull(p); } } }
Finally, in my test project, I have a test object object.
using Playground.Model; namespace Playground.UnitTests { public class PlaygroundEntitiesTest : PlaygroundEntities { private PlaygroundEntitiesTest() { } public PlaygroundEntitiesTest(string connectionString) : base(connectionString) { } } }
In my model project, I have an entity and my context.
Playground.cs
using System; namespace Playground.Model { public class Playground { public Guid Id { get; set; } public string Name { get; set; } public string Location { get; set; } } }
PlaygroundEntities.cs
using System.Data.Entity; namespace Playground.Model { public class PlaygroundEntities : DbContext { public PlaygroundEntities() : base("PlaygroundConnectionString") { } public PlaygroundEntities(string connectionString) : base(connectionString) { } public virtual DbSet<Playground> Playgrounds { get; set; } } }
Finally, I installed the post-build step in the unit test project to execute the command to initialize LocalDb as follows:
Full team
"C: \ Program Files \ Microsoft SQL Server \ 120 \ Tools \ Binn \ SqlLocalDB.exe" create "v12.0" 12.0 -s
Then it was as simple as clicking on Visual Studio Online and starting my build.
