I am new to unit testing so please can anybody tell me difference b/w Mockito and TestNG. And which tool will be suitable for unit testing.
Both tools are suitable for unit testing and you can use them together. TestNG is similar to JUnit but introducing some new functionalities, and Mockito allows you to add mock objects to your tests.
Related
I'm in the middle of implementing a unit test infrastructure for a large C++ project, and due to political reasons I'm almost sure CppUnit will be pushed as the unit testing framework.
I'm trying to identify mock frameworks that blend with CppUnit. I've found mockpp, and I've heard that Google Mock should work.
What frameworks work alongside CppUnit?
Mocking libraries are typically independent of the unit testing framework. They accomplish two different jobs, and frankly don't have much reason to talk to each other. Where they do integrate is in answering these questions:
When should I create my mock objects?
When should I initialize the mock objects with my expectations?
When should I validate the mocks were called as expected?
And you do that at the appropriate points in your tests.
For one example, check opmock. http://sourceforge.net/projects/opmock/
According to their wiki, opmock is easily called from a CppUnit test. See http://sourceforge.net/p/opmock/wiki/Using%20Opmock%20with%20other%20unit%20testing%20frameworks/
I am completely baffled while trying to use EasyMock. Does anybody know of a (very) gentle introduction to EasyMock?
I already heavily use TDD, and I use mocking (which I guess is almost a prerequisite for doing TDD). I have just never used a framework for mocking before.
Here are some articles you might like:
Easier testing with EasyMock
Unit testing with JUnit and EasyMock
Mock Object Testing With EasyMock 2
Getting Started with EasyMock2
You could also check out other mocking frameworks such as Mockito, PowerMock and JMockit.
How about their own Documentation? http://easymock.org/EasyMock3_0_Documentation.html
Have you tried this tutorial? http://www.vogella.de/articles/EasyMock/article.html
Do you already have a background in Test Driven Development? If not, I'd get my hands on a copy of Roy Osherove's book The Art of Unit Testing. http://artofunittesting.com/
BDD has been touted as "TDD done right".
However TDD is widely used with unit tests, rather than end-to-end integration tests.
Which kind of test is most appropriate for BDD?
Should we write only integration tests?
Should we also write unit tests?
If yes, should there be multiple unit-tests per scenario?
And what a unit test covers multiple scenarios? Is there a way to structure these tests when using a testing framework such as MSpec?
Which kind of test (integration tests, unit tests) is most appropriate for BDD?
I would use both in two nested loops as described in Behavior-Driven Development with SpecFlow and WatiN
* writing a failing integration tests
* writing a failing unit test as part of the solution of the integration test
* making the unittest pass
* refactor
* writing the next failing unit test as part of the integration test
* unitl the integration test passes
* writing the next failing integration tests
The reason you often see acceptance/integration tests used in the BDD cycle is because many of it's practitioners put a heavy emphasis on mocking and outside-in development. As such they tend need to include both end-to-end integration/acceptance tests as well as unit tests to ensure the total behavior of the system.
When fake objects are being used as collaborators instead of real objects in a unit test, it is quite easy to verify that the isolated unit under test behaves in the proper fashion (that is that it modifies it's state properly, and sends the proper messages). However it can not verify anything more than that in isolation the individual object behaves as expected. As such, an end-to-end acceptance test is then necessary verify that all the objects in the system when used together provide the promised value to the end user.
So basically within this approach unit tests and acceptance tests play the following roles:
Unit Tests - Objects in isolation behave in the proper manner
Acceptance/Integration Tests - All Objects together provide the promised value of the system.
Although I myself am a large proponent of this style of development, it is somewhat independent from the general BDD ideas. In my opinion acceptance/integration tests are only necessary when your isolating your system under test in your unit tests by using mocks and stubs.
I'll give the answer that seems most likely to me, but I might be off-track here.
TDD was originally implemented with unit-tests, which clarify requirements for individual components of the software by calling them and expecting certain results.
BDD simply applies the same concept to the system as a whole. So for BDD, you write integration tests, which call the system from the outside-in, by simulating mouse clicks, etc, or whatever interface the end-user uses to access the system.
BDD seems to actually be a kind of black-box/UAT testing, since it's concerned with the behaviour of the system as a whole, not with the means by which the behaviour is implemented.
So it seems advisable to write unit tests and integration tests, but at the very least, integration tests, so that if we don't have time to verify each component, we can at least verify the system as a whole.
The basic idea is to write the tests before the code, so for any feature, use the test appropriate to examine the feature. If the feature you are adding is "clicking on the report bug link should send an email", then an integration test seems appropriate (presumably with some unit tests for components of that feature). If the feature you are working on is "Average usage calculation should omit the highest and lowest value", a unit test is probably more appropriate. The important thing is to accurately be able to tell when what you are building is done, and later to be sure changes didn't break it. The rest is just bookkeeping.
For those of us who like to learn by reading good code, what are some of the projects you've seen which provide a great example of a medium to large suite of unit tests in action?
This isn't a question about what your favourite unit testing framework is, but it could be helpful to add which unit testing and/or mocking frameworks are used by the projects you mention.
Any platform will do, but I'm mainly interested in projects which use an xUnit-style unit testing framework in .NET.
Apache CXF has a mind-numbingly large array of unit tests. They are written to Junit, and include the JUnit-Spring mechanisms as well as one of the mock libraries. They include launching processes and many other mechanisms you might need some day.
For example, ReportLab uses Python's unittest module (also known as PyUnit) and has lots unit test that you might want to take a look at.
I'm also using unittest myself. It is very similar to Java's jUnit framework and thus very handy for writing tests in no time.
The Service Factory has about 780 unit tests, including some that test code meant to run inside of Visual Studio. It has some very good examples of mocking such code.
The Enterprise Library has even more.
NunitLite has a unit tests suite but I'm not sure it qualifies as a medium-sized project. I seem to remember NUnit also has unit tests, but its site currently is unavailable.
Like anything else understanding the words makes it much easier to learn the language. Can anyone chime in with all the words used in unit testing with their definitions (ex. Mock, Fixture, etc. )
This looks like a great page: http://xunitpatterns.com/Glossary.html
It includes:
SUT
synchronous test
task
TDD
test automater
test case
test code
test condition
test context
test database
test debt
test driver
test driving
test error
test failure
test fixture
test fixture
test fixture
test maintainer
test package
test reader
test result
test run
test smell
test stripper
test success
test suite
test-driven bug fixing
test-driven development
test-first development
test-last development
test-specific equality
test
In relation to mocking etc this might be useful:
This table and its references might be more useful:
http://xunitpatterns.com/Mocks,%20Fakes,%20Stubs%20and%20Dummies.html
Perhaps these articles will be more helpful:
Wikipedia:
In computer programming, unit testing is a software design and development method where the programmer gains confidence that individual units of source code are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual program, function, procedure, etc., while in object-oriented programming, the smallest unit is a method, which may belong to a base/super class, abstract class or derived/child class.
Unit testing can be done by something as simple as stepping through code in a debugger; modern applications include the use of a test framework such as xUnit.
Ideally, each test case is independent from the others; Double objects like stubs, mock or fake objects1 as well as test harnesses can be used to assist testing a module in isolation. Unit testing is typically done by software developers to ensure that the code other developers have written meets software requirements and behaves as the developer intended.
MSDN:
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. Each unit is tested separately before integrating them into modules to test the interfaces between modules. Unit testing has proven its value in that a large percentage of defects are identified during its use.
Extreme Rules:
Unit tests enable collective code ownership. When you create unit tests you guard your functionality from being accidentally harmed. Requiring all code to pass all unit tests before it can be released ensures all functionality always works. Code ownership is not required if all classes are guarded by unit tests.
I've also found a glossary of testing terms, but it doesn't define Mock or Fixture, but there's an option to add new ones. Once the question is answered to your satisfaction maybe that could become the canonical source.
Mock,
n.
A type of turtle, used primarily in soup.
A code construct used in Unit Testing, named after #1. A mock looks like the real thing to the code being tested, however any attempts to interact with it result only in mournful songs.
v.
To construct a mock for use in testing.