mercredi 1 juillet 2015

MVC 5 Unit tests vs integration tests

I’m currently working a MVC 5 project using Entity Framework 5 (I may switch to 6 soon). I use database first and MySQL with an existing database (with about 40 tables). This project started as a “proof of concept” and now my company decided to go with the software I’m developing. I am struggling with the testing part. My first idea was to use mostly integration tests. That way I felt that I can test my code and also my underlying database. I created a script that dumps the existing database schema into a “test database” in MySQL. I always start my tests with a clean database with no data and creates/delete a bit of data for each test. The thing is that it takes a fair amount of time when I run my tests (I run my tests very often).

I am thinking of replacing my integration tests with unit tests in order to speed up the time it takes to run them. I would “remove” the test database and only use mocks instead. I have tested a few methods and it seems to works great but I’m wondering:

  1. Do you think mocking my database can “hide” bugs that can occur only when my code is running against a real database? Note that I don’t want to test Entity Framework (I’m sure the fine people from Microsoft did a great job on that), but can my code runs well against mocks and breaks against MySQL ?
  2. Do you think going from integration testing to unit testing is a king of “downgrade”?
  3. Do you think dropping Integration testing and adopting unit testing for speed consideration is ok.
  4. I’m aware that some framework exists that run the tests against an in-memory database (i.e. Effort framework), but I don’t see the advantages of this vs mocking, what am I missing?

I’m aware that this kind of question is prone to “it depends of your needs” kind of responses but I’m sure some may have been through this and can share their knowledge. I’m also aware that in a perfect world I would do both (tests by using mocks and by using database) but i don’t have this kind of time.

As a side question what tool would you recommend for mocking. I was told that “moq” is a good framework but it’s a little bit slow. What do you think?

Regards

Aucun commentaire:

Enregistrer un commentaire