I'm looking for a good introduction/tutorial for unit testing C#. Most tutorials I've come across so far have either been too basic to be useful or too complex for someone new to unit testing.
(Using Visual Studio 2008 Professional for Windows applications)
Read The Art of Unit Testing by Roy Osherove. It is very good.
Is it just a specific tool for which you're having trouble finding good tutorials? When I was new to the subject I found the NUnit tutorial to be a good starting point:
http://www.nunit.org/index.php?p=quickStart&r=2.4
Rhino Mocks would be good to learn as well to complement the unit testing framework:
https://stackoverflow.com/questions/185021/rhino-mocks-good-tutorials
Perhaps a book? I would recommend you the Pragmatic Unit Testing in C# with NUnit.
It's very complete in my opinion.
It was when I started reading about Moq that I realized unit testing didn't have to be painful. There are some good links near the bottom of the page as to how unit tests can be built with mocking.
One nice thing about using interfaces for controlled coupling and testing is that adding an interface to an existing code base is not a breaking change. I'm adding new features to some legacy code and I've been creating interfaces for existing classes so that the new features can be developed and tested in isolation. It's been working well so far and I plan to continue this style of testing on other projects. For me, the key was to avoid designing complex stub classes with lots of ugly conditional code to expose different cases for my tests. It got to the point where the test code was so complex that I couldn't be sure if it was the code or the test that was broken.
Related
I have looked into TDD, unfortunately the project has had most of the development completed, and there isn't any point in implementing it now. However, the project has not been deployed, and there will be changes every week to add to the existing code.
Are there any types of tests I can add to the site that I can run daily to ensure my code is always working? In case other people starting development on the site, or any new code is added, or old code is edited?
Of course there are types of tests you can add:
Unit tests - testing classes in isolation (assuming a DI structure to the code)
Integration tests - testing interactions between classes (typically two classes)
UI tests - using automation (selenium for example, to automate the browser) to test the application from UI through to the database
Performance testing
It is best to concentrate on areas of change (bug fixes, feature requests) in order to get the best bang for the buck.
Nitpick: TDD is a design methodology using testing frameworks, not a testing methodology.
If there will be weekly changes, "the project has had most of the development completed" is probably not a true statement. Most of the development, in fact, still lies ahead.
It is still very worth introducing unit tests. I would suggest creating new tests each week that cover the areas of code that undergo maintenance.
Well, you wouldn't be implementing TDD if you are retro fitting tests into a legacy codebase. I would suggest investing time in producing at least unit tests for new code. Hopefully your code will encroach on legacy code which you can then place unit tests around. Baby steps is the key with legacy code. There is no hard and fast way to do this.
Unit tests can always be added, and it's always a good idea to add them. I would suggest NUnit in your case, because it seems to work really well with existing code. It's also really easy to use.
I've recently started reading The Art of Unit Testing, and the light came on regarding the difference between Unit tests and Integration tests. I'm pretty sure there were some things I was doing in NUnit that would have fit better in an Integration test.
So my question is, what methods and tools do you use for Integration testing?
In my experience, you can use (mostly) the same tools for unit and integration testing. The difference is more in what you test, not how you test. So while setup, code tested and checking of results will be different, you can use the same tools.
For example, I have used JUnit and DBUnit for both unit and integration tests.
At any rate, the line between unit and integrations tests can be somewhat blurry. It depends on what you define as a "unit"...
Selenium along with Junit for unit+integration testing including the UI
Integration tests are the "next level" for people passionate about unit testing.
Nunit itself can be used for integration testing(No tool change).
eg scenario:
A Unit test was created using Nunit using mock(where it goes to DB/API)
To use integration test we do as follows
instead of mocks use real DB
leads to data input in DB
leads to data corruption
leads to deleting and recreating DB on every test
leads to building a framework for data management(tool addition?)
As you can see from #2 onwards we are heading into an unfamiliar territory as unit test developers. Even though the tool remains the same.
leads to you wondering, why so much time for integration test setup?
leads to : shall I stop unit testing as both kinds of tests takes
more time?
leads to : we only do integration testing
leads to : would all devs agree to this? (some devs might hate testing altogether)
leads to : since no unit test, no code coverage.
Now we are heading to issues with business goals and dev physco..
I think I answered your question a bit more than needed. Anyways, like to read more and you think unit tests are a danger? then head to this
1) Method: Test Point Metrics is best approach in any environment. By this approach not only we can do unit and integration testing but also validate the requirements.
Time for writing Test Point Metrics is just after the Requirement understanding
A template of Test Point Metrics available here:
http://www.docstoc.com/docs/80205542/Test-Plan
Typically there are 3 kind of testing.
1. Manual
2. Automated
3. Hybrid approach
In all above cases Test Point Metrics approach works.
2) Tool:
Tool will depend upon the requirements of project anyhow following are best tools according to my R&D
1. QTP
2. Selenium
3. AppPerfect
For more clear answer about tool, please specify your type of project.
Regards:
Muhammad Husnain
I mostly use JUnit for unit testing in combination with Mockito to mock/stub out dependencies so i can test my unit of code in isolation.
For integration tests these are normally involve 'integration' with an external system/module like a database/message queue/framework etc... so to test these your best bet would be the use a combination of tools.
For e.g. i use JUnit as well but rather than mock out the dependencies i actually configure those dependencies as it were calling code. In addition, i test a flow of control so that each method are not tested in isolation as it is in Unit testing but instead together. Regarding Database connectivity, i use an embedded database with some dummy test data etc.
I've recently been studying TDD, attended a conference and have dabbled in few tests and already I'm 100% sold, I absolutely love it TDD.
As a result I've raised this with my seniors and they are prepared to give it a chance, so they have tasked me with coming up with a way to implement TDD in the development of our enterprise product.
The problem is our system has evolved since the days of VB6 to .NET and implements a lot of legacy technology and some far from best practice development techniques i.e. a lot of business logic in the ASP.NET code behind and client script. The largest problem however is how our classes are tightly coupled with database access; properties, methods, constructors - usually has some database access in some form or another.
We use an in-house data access code generator tool that creates sqlDataAdapters that gives us all the database access we could ever want, which helps us develop extremely quickly, however, classes in our business layer are very tightly coupled to this data layer - we aren't even close to implementing some form of repository design. This and the issues above have created me all sorts of problems.
I have tried to develop some unit tests for some existing classes I've already written but the tests take A LOT longer to run since db access is required, not to mention since we use the MS Enterprise Caching framework I am forced to fake a httpcontext for my tests to run successfully which isn't practical. Also, I can't see how to use TDD to drive the design of any new classes I write since they have to be so tightly coupled to the database ... help!
Because of the architecture of the system it appears I can't implement TDD without some real hack which in my eyes just defeats the aim of TDD and the huge benefits that come with.
Does anyone have any suggestions how I could implement TDD with the constraints I'm bound to? Or do I need to push the repository design pattern down my seniors throats and tell them we either change our architecture/development methodology or forget about TDD altogether? :)
Thanks
Just to clarify, Test driven development and unit testing are not the same thing.
TDD = Write your tests before your code.
Unit Testing = Writing tests that confirm a small unit of code works.
Integration Testing = Writing tests that confirm blocks of code work together.
TDD, by definition, can't be done on existing code. You've already developed the code, so you aren't going to develop it again. TDD means you first write a failing test, then you write just enough code to pass the test. You've already written the code, so you can't do TDD.
You can write unit tests for existing code but this isn't the same as doing TDD.
The tests you have described (accessing the database etc) are technically integration tests. Integration tests do usually take ages to run. A true unit test would purely test your DA layer code without actually accessing the database. True unit testing requires interfaces to test against so you can isolate units from the surrounding units.
It's very hard to properly unit test existing code, unless it's been well designed with interfaces and abstraction in mind.
I know I'm being slightly picky with terminology here, but it's important when it comes to what approach you can take. My advice would be that with existing code that isn't well abstracted you should gradually build up a suite of automated integration tests. When you come to write something new (Which may not be a whole project, it may just be a new part to the existing app), consider approaching it in a TDD style. To do this you will find that you need to write some interfaces and abstractions to allow you to do TDD on your new code without triggering too much of the existing code. You will then be able to write some more integration tests that test your new code working with the old code.
To cut it short - don't try and change methodology for existing code. Just do new code better.
Nitpick: you can't do Test-Driven Design on existing code, but I do realize that you wish to use TDD against new functionality you implement on the existing code base.
The best thing you can do is to gradually introduce seams into the code base, shielding you from the tightly coupled code already in existence.
The book Working Effectively with Legacy Code contains much good advice on how to turn a legacy application into a testable application.
I think the mere fact that you are using ASP.NET Web Forms your road to TDD will be challenged. It's just a nightmare to mock the Session/ViewState/HTTPContext in Web Forms so test-driven is almost a hindrance. There is of course the ASP.NET MVC alternative, but you're way down the road already it seems. Another promising option for the Web Forms paradigm is ASP.NET Web Forms MVP, but that project is really not fully mature yet. I'm moving everything I can to MVC; the other option for TDD with Web Forms, Selenium, isn't really TDD as it should be.
For adding tests to your existing code, you might check out Working Effectively With Legacy Code. "Legacy code" is defined as code that does not have tests in it.
Can any one suggest a step by step example for using moQ framework.
any guidelines or thumbrules that has to be followed while mocking objetcs . can be much help.
thanks.
Here's the moq quick-start
Update: To address your comment... A large part of writing testable code involves removing dependencies on classes/resources outside the scope of your control. A very common approach to doing this is by talking to interfaces instead of concrete examples.
It's a little much to describe properly (especially since I just rolled out of bed), so let suggest you pick up a copy of Roy Osherove's 'The Art of Unit Testing'. It's a fairly short book and is filled with good advice and lots of summary information to get you familiar with many of the approaches to unit testing.
I only started to use Moq recently and I am not sure how much help this will be but if you can get your hands on Chapter 3 of Pro ASP.NET MVC Framework there is a really good step-by-step example of using moq and NUnit with ASP.NET MVC.
What are the good resource to learn BDD & TDD (ruby , C#, javascript).
What are the good framework using now?
See
Why should I practice Test Driven Development and how should I start?
Beginning TDD - Challenges? Solutions? Recommendations?
Good C# Unit testing book
Introducing BDD
What is the Path to Learn BDD on Ruby On Rails?
Jasmine
Hanselminutes - Understanding BDD and NSpec
I can't really speak with too much authority on this subject, nor will I speak with too greater vigour given how storongly people feel about those two acronyms but it seams as though you are new to BDD / TDD so I will tell you what I wish someonehad told me when I first encoubtered them.
Tests and tools are one component of the overall process and methodology behind TDD, it is arguably one of the more visible aspects and so it is likely that you will see a lot of references to testing being an importabt part of TDD, and it is, I'm just saying, if you are interested in learning about TDD, don't forget the other aspects of it as well.
Resources I have found helpful:
The Pragmatic Programmer: doesn't
really talk about TDD per se, but it
does emphisise a lot of other good
practices that will help you.
Hanselminutes Episode 169
Tools I find helpful
TestDriven.NET - test runner in
visual studio
xUnit.NET - Test Framework
Rhino Mocks - Isolation (mocking)
framework
Here are a few links that may be helpful to you.
What is the most mature BDD Framework for .NET?
Introduction to BDD and Mocking
BDD using NUnit and Moq
RSpec vs Cucumber (RSpec stories)
BDD with Cucumber and rspec - when is this redundant?
NSpec Project Site
Continuous Testing
IMO, the best way to learn these days is with plurasight
http://www.pluralsight-training.net/microsoft/
my boss pays for me and my fellow developer to access the videos, but even if he didnt, its thats good id pay for it myself.