I need to write integration tests for my layer that exposes methods of service. But I need my database to be in a certain state for the tests to pass
For example for testing the GetStoreByID method, I need to have store 1 in my database but not store 2 (for the ko test)
The database is developed and deployed by a another team using a sql project (dacpac)
I use Entity Framework 6.1.3 with an Edmx
What is the best way, in this case, to setup the data in database before tests ?
This link https://msdn.microsoft.com/en-us/library/dn314431(v=vs.113).aspx gives the Microsoft MSDN article on how to write tests using EF6 by faking out the database.
In summary the article covers the subject 'When writing tests for your application it is often desirable to avoid hitting the database. Entity Framework allows you to achieve this by creating a context – with behavior defined by your tests – that makes use of in-memory data.'
If your data access is separated by an interface (i.e. using something like IDBSet for each of the types), personally I'd avoid depending on the database completely where possible, and use either a fakedbsets (if you need to test repository/DAL code) or just mocking with moq or nsubstitute if you don't.
I know this doesn't specifically answer your question, but the best way to setup test data is do it in memory in my experience with as few external dependencies as possible. The database adds extra moving parts that you don't really want to have to depend on in unit/integration tests. This also adds complexities if you have a CI server etc... that you generally want to avoid.
Related
I have written a .Net application which has many components, some of those components are database access layers which abstract from the rest of the components where the data comes from.
I have unit tested the rest of the components by mocking the database access layer. One way I have of testing the database access layers is to use create new empty databases on the test servers. This can be slow, and most would argue that it is not a unit tests as I depend on the database server.
What I think I want, is a mocked database which I can use to test my database access layer. The mocked database can be given a schema and process SQL commands as if it were a remote database, but in fact it is all in-memory. Does this exists? Or how else can I test my SQL and database <-> data model code.
To solve my problem you may want to know I am using SQL Server, versions 2008 and later, and my code is written in C#, running with .Net 4.5 and using Visual Studio 2013
Note: I do not want to use Linq2SQL/EntityFramework to replace my database access layer, as in my experience it results difficult to debug issues and performance problems.
I tried to phrase my question carefully to avoid people lecturing me on their beliefs in what should be tested and how, but perhaps to be a little more blunt:
I want to unit test my SQL, small changes to that have a big impact on the outcome of the program. I do have integration tests, but it takes much longer to create a release for the test environment than it does to tweak code and run the unit tests.
I appreciate people taking the time to read my question and respond anyhow.
I don't know if it's going to be the best answer, but. The way we're doing is that we're using SQLite, which is an in-memory database. There are a number of different ways to set it up, we use NHibernate as an ORM, and for that it is fairly easy to set up using FluentNHibernate, but I don't think it's much harder using any other framework either:
Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory())
.Mappings(m => ... )
.BuildConfiguration()
.BuildSessionFactory();
I think you can run queries against a SQLite database without any ORMs as well, e.g. using the base SqlConnection class.
This database can accept migrations, schemas, etc. It behaves very close to a proper MsSql database, we can run any DDL an DML statements against it, and it works fine. Since it's all in-memory, it's also pretty fast.
How to unittest a fairly simple application?
By "fairly simple application" understand:
An application that queries a database (that can be complex) through an ORM and displays the result without any further processing. That's, the Linq query returns a simple IEnumerable that will be displayed on the screen.
The kind of tests I'm writing so far tends to be slow and I delay more and more their execution. If I keep this process, these tests will only be executed when I commit the code and thus I'll loose all the benefits of unit test: highlight bugs or errors within a minute.
I tried to make an object graph in memory but as the application grows this graph become very complex and it's taking more and more time to be maintained. And when I plug the ORM instead of object graph, I see bugs my unit tests didn't foresee...
In my case, I need a generic solution as the ORM should be either Entity Framework or nHibernate.
EDIT:
I rephrase my question as it is maybe a bit unclear: test this kind of application means test the queries. What is the best manner to test Linq queries?
The simple answer to your question is not to unit test data queries. As you found out the value of unit testing this part of the application is very limited and most of the bugs don't show up. The ORM is a good place for integration testing.
To enable this your data access code must be put in one place. Depending on your application this may be a data access layer, a Repository or any other structure that enables you to split your business logic from your data access. In the business logic part you use unit tests and mock or replace the data access with an in memory solution.
The data access part you test with integration tests. But here you only have to test if you can insert, update, select or delete data and if your queries work. That should be doable with way less tests than if you would try to do this all within your unit tests.
Entity Framework and nHibernate both have tutorials on how you can use those frameworks for this kind of testing.
Johnny Graber's answer is similar, but I'll add mine anyway for sake of several opinions.
To answer specifically on your issue with testing Linq queries (and still, this is my opinion, like in the comment above): you don't test them.
As nice and simple Linq is, it is basically as bad having a lot of Linq queries in a project as it is to have hardcoded SQL statements.
Hence, the Linq queries should be part of a data abstraction layer and that layer is what you test. That layer provides basic access methods, mostly CRUD operations. These operations might be implemented using Linq against whatever (SQL, SQLite.Net, XML, you name it).
You should then have another implementation of your DAL which returns/generates mock data. No need to use Linq here.
You can then test, if you're application works against the implementation that contains mock data, which is fast, as it's memory based.
You worry about the outcome of these methods.
You can from time to time switch over to the productive version that's based on Linq and run the slower tests.
Linq itself works. Microsoft did a pretty good job. You need to worry about your access methods returning proper data.
I have a time consuming operation that needs tested. The operation makes changes to a database. Current, the majority of my tests are very similar, a query is performed on the database to determine the expected result after the operation. Then, the operation under test is ran. Finally, the database is again queried to determine if the expected condition has been met. Since I am only querying the database in each test to determine the expected and actual results, and the function under test is always performing the same operations on the database. Is there a way that I could run the operation once and still test each condition in its own test?
The standard strategy for running unit tests on code that has side effects such as connecting to databases is to provide mock objects instead of a real database connection or other service, but it depending on how your code is structured this may be harder or easier to adopt.
Frameworks for inversion of control and dependency injection can be very helpful for this kind of purpose. The dependancy injection pattern makes sure that objects can have dependant implementations passed to them at runtime, rather than constructing them directly, for testing purposes or just to create more modular code. StructureMap is such a framework for C#, but there are others, such as Spring.NET.
Sounds like you want to be using Unit Testing Frameworks and/or Mocking Frameworks.
As a general rule, you shouldn't be writing additional queries to test the query has worked as expected on the database as these themselves will require development and testing.
Depending on the database you're writing to, you may be able to get the number of rows affected on the database (take a look at ##rowcount for SQL Server). But again, you're adding additional code just to do testing which should really be a separate activity.
I am still having a issue getting over a small issue when it comes to TDD.
I need a method that will get a certain record set of filtered data from the data layer (linq2SQL). Please note that i am using the linq generated classes from that are generated from the DBML. Now the problem is that i want to write a test for this.
do i:
a) first insert the records in the test and then execute the method and test the results
b) use data that might be in the database. Not to keen on this logic cause it could cause things to break.
c) what ever you suggest?
You should choose option a).
A unit test should be repeatable and has to be fully under your control. So for the test to be meaningful it is absolutely necessary that the test itself prepares the data for its execution - only this way you can rely on the test outcome.
Use a testdatabase and clean it each time you run the tests. Or you might try to create a mock object.
When I run tests using a database, I usually use an in-memory SQLite database.
Using an in memory db generally makes the tests quicker.
Also it is easy to maintain, because the database is "gone" after you close the connection to it.
In the test setup, I set up the db connection and I create the database schema.
In the test, I insert the data needed by the test. (your option a))
In the test teardown, I close the connection to the db.
I used this approach successfully for my NHibernate applications (howto 1 | howto 2 + nice summary), but I'm not that familiar with Linq2SQL.
Some pointers on running SQLite and Linq2SQL are on SO (link 1 | link 2).
Some people argue that a test using a database isn't a unit test. Regardless, I belief that there are situations where you want automated testing using a database:
You can have an architecture / design, where the database is hard to mock out, for instance when using an ActiveRecord pattern, or when you're using Linq2SQL (although there is an interesting solution in one of the comments to Peter's answer)
You want to run integration tests, with the complete system of application and database
What I have done in the past:
Start a transaction
Delete all data from all the tables in the database
Setup the reference data all your tests need
Setup the test data you need in database tables
Run your test
Abort the transaction
This works well provided your database does not have much data in it, otherwise it is slow. So you will wish to use a test database. If you have a test database that is well controlled, you could just run the test in the transaction without the need to delete all data first.
Try to design your system, so you get mock the data access layer for most of your tests. It is valid (and often useful) to unit test database code, however the unit tests for your other code should not need to touch the database.
You should consider if you would get more benefits from “end to end” system tests, with unit tests only for your “logic” code. This depend to an large extent on other factors within the project.
I have an existing ASP.NET MVC application with some sample data in the SQL Server database, which is working fine..
Assuming I have all of the necessary repositories and IOC in place, is there a tool that will extract the data from a group of tables, and "freeze-dry" it into a mock object (perhaps using an XML file to store the data), so that I can detach the database and use the mock data for my unit tests?
Depending on what exactly you are trying to test there might be different approaches.
If you want to test the data access logic then this is no longer unit test but integration test. For this kind of tests it is a good idea to be able to easily replace the real database with some lighter maybe even in-memory database like SQLite which will be reconstructed before running each test. If you are using an ORM this task is easy. All you need to do is to generate SQL scripts (INSERT INTO...) from your existing database, modify and adapt the dialect to SQLite (if necessary), read and inject into a SQLite file and finally all that's left is to instruct your data access layer to use SQLite dialect and connection string for the unit test.
Now if you are not using an ORM and your data access logic is tied to MSSQL things get uglier you will need a live database in order to perform those integration tests. In this case I would suggest you duplicate your real database which you would use for the tests by modifying only the connection string. Once again you will need to properly setup and teardown (preferably in a transaction) this test database in order to put it into a known state for the tests.
If you want to test code that depends on those repositories (such as your controllers for example) you don't need to even bother about mocking the data as your controllers depend on abstract repositories and not the real implementations (aren't they), so you could easily mock the methods of the repository in order to test the logic in the controllers.
This is actually a well known "test smell":
http://xunitpatterns.com/Obscure%20Test.html#General
From:
2098937: Proper way to Mock repository objects for unit tests using Moq and Unity
I don't know of a direct way to do what you're asking for, but MSSQL supports export to CSV, Access, and Excel. Although, this require you to change the Data Access Layer in your in your mid-tier, and furthermore, I don't feel this answers your question:
"freeze-dry" it into a mock object
This I'm not sure of. Is it feasible to just restore a backup of the database either on the same SQL server as a new database, or possibly on a dev SQL server?