Is there anything wrong with putting test methods in the same class as the class you're testing?
So add the [TestFixture] attribute to every class and have [Test] method alongside the actual methods.
I like the idea. Keeps everything in one place.
But what, if any, are the downsides to this ?
/I use Nunit and c#.
We can mock runInGround(String location) method inside the PersonTest class as shown below. Instead of using mock(class) here we need to use Mockito. spy() to mock the same class we are testing. Then we can mock the method we want as follows.
The short answer is that you shouldn't test private methods directly, but only their effects on the public methods that call them. Unit tests are clients of the object under test, much like the other classes in the code that are dependent on the object.
If your class is simple, you shouldn't feel the need to mock out pieces of it during a test. If your class is complex enough that you feel the need to mock, then you should break the class up into simpler pieces.
Yes it is very bad practice - you're letting your tools make design decisions for you. I think the main problem here is that you're trying to treat each individual method as a unit. This is generally the cause of all unit test woes.
Yes this is a pattern to be avoided. Tests should be a completely separate entity from your shipping code for at least the following reasons
private
/ protected
constructs. Instead they are forced to go through the public API pointsThis approach grates my soul.
I think it makes a lot more sense to keep your test code separate from your production code. Many reasons:
Most of all:
It just plains feels / smells wrong.
Yes. It would be breaking Single Responsibility Principle. It will reduce the readability. You add a dependency to your testing framework.
Biggest downside: you have to ship your tests, and they potentially become part of your public API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With