0

I know that Unit Testing is all about testing individual units of code and mocking the other dependencies like database connections, file I/O etc.

I have a few questions on unit tests

  1. what is that "UNIT" in unit testing that we need to test? Is it a method of a class? or a use-case? Because use-case testing seems more of an integration test to me.
  2. Mocking external dependencies. Does it mean to mock all references to any external class even if it is same package?
AgentX
  • 1,402
  • 3
  • 23
  • 38
  • There are plenty of articles about unit testing that you can find from google please try to ask specific technical questions. – Ronan Quillevere Jul 06 '15 at 12:30
  • Why the downvote? Yes, it is a beginners question. But it is good formed and asks about specific things interesting for someone starting with unit tests. – jwsc Jul 06 '15 at 12:30
  • 3
    IMO this question is way too broad, this topic is well documented on the internet. That is the reason of my downvote despite the quality of the formalism. – Ronan Quillevere Jul 06 '15 at 12:32
  • I hope I made the question a bit specific? – AgentX Jul 06 '15 at 12:38

3 Answers3

2

The 'Unit' in unit testing is not well defined.

However it is often used to refer to the process of testing an individual function or method. Unit tests like these are often combined into a group which tests a group of related functions or methods (e.g. a class).

As you suspect, the unit under test should be isolated from any other code during testing. This allows you to be sure that any error occurs within the code you wish to test.

For a much better and more detailed explanation, I recommend Martin Fowler's post on the issue of Unit Testing, which directly addresses this issue:

Unit testing is often talked about in software development, and is a term that I've been familiar with during my whole time writing programs. Like most software development terminology, however, it's very ill-defined, and I see confusion can often occur when people think that it's more tightly defined than it actually is.

C Hudson
  • 21
  • 1
  • Thanks , I will check that out. But this post here clearly answers my question: http://stackoverflow.com/questions/10752/what-is-the-difference-between-integration-and-unit-tests?rq=1 – AgentX Jul 06 '15 at 13:09
1

what is that "UNIT" in unit testing that we need to test? Is it a method of a class? or a use-case?

A chip is a unit to a RAM. Ram is a Unit to Motherboard and motherboard can be a unit in a border context. So there is no specific answer for your question. But mostly we use methods as units because they are the smallest elements that we can test. Sometimes even a class or a bigger component can be a unit. It depends on your context and how important and complex that particular "unit" is?

Mocking external dependencies. Does it mean to mock all references to any external class even if it is same package?

You mock if you do not have direct access to some component. (class...etc) It doesn't matter where it is. If you want to test something which depends on something that hasn't been implemented yet, you need to mock. What should mock is depend on what you want to test!

(ex. I want to implement a Tsunami alert system but the hardware (which send the wave signal) which should be in the sea is not implemented yet. So if I want to test the alert system I built I may be need to mock the behavior of that hardware device)

nilan59
  • 1,006
  • 9
  • 24
0
  1. Independent unit testing refers that each test should not depend on the result of other test.

  2. Mock external dependencies is replacing complex dependencies for a simple one: I.E replace a DataBase to a Mock that hardcode the result.

Hope it helps.

  • Thanks, but that is already clear to me. I recently got confused in what's a "UNIT" in Unit Testing? and do we need to mock everything outside the scope of class under test? – AgentX Jul 06 '15 at 12:44
  • With unit testing you are testing a part of the code. You should mock only the objetcs that are hard to incorporate, to get a specific value, etc. I.E: You have a test that prints "Critical" when the temperature is above 80 degrees. You have a web service that can obtain the actual temperature. In the test you should mock the web service and return the temperature that you need to do the test. (If you are going to test the "Critical" message you are going to mock the service to always return a value above 80). Sorry for the bad english. It always depends on the context. – Juan Marcos Armella Jul 06 '15 at 12:55