Where do we start using unit testing?

Where do we start using unit testing?
I have some doubts as to where to start using unit testing.
I am doing unit testing with Junit in RAD. And I do this after the code is ready to be deployed or maybe after deployment. I am confused as to why we are unit testing after the code is almost ready to deploy. My question is when should we start unit testing?

I have some more questions .....
What am I doing in my unit testing, I took one method from a class and create a test class for that method.
In this class, I give some input values ​​to the method and expect respectable output values ​​from the database.
Now here the only test class accepts input values ​​-> passes it to the method -> calls the method from the original class -> connect to the database -> retrieves the value from DB -> returns it to the test class.

If the test runs successfully, the Junit console displays a green bar another red one. Red bar with the reason for the error. But it doesn't generate a unit test report.

Now here's my question ...
Am I doing correct unit testing? Since one unit test method contains all the code flow and gives the result ...

0


a source to share


7 replies


The best time to start unit testing, if you haven't already, is now. The most effective use of unit tests is Test Development (TDD), where you write tests for each method as you implement it (write a test that fails and then implement the method to make it pass). However, it is not too late to add tests later. JUnit tests simplify certain assumptions about code that might change later. If the assumption changes, the test is aborted and you can save on some errors that could be very difficult to detect.

I don't know about the report object, but you can add a JUnit ANT task that will output the test results to the build console (or log if ANT output is received).

Your database tests sound like small integration tests, not unit tests. This is fine, but if the tests are too slow, you might want to use mock objects (via a framework like JMock or EasyMock) to replace real connections with fake ones. This isolates the method logic from the state and behavior of the database, and allows you to run tests without worrying about whether the database is running and the test data is stored.

Useful links:

http://en.wikipedia.org/wiki/Test-driven_development

http://www.jmock.org/



http://easymock.org/

http://ideoplex.com/id/25/ant-and-junit

http://ant.apache.org/manual/Tasks/junit.html

http://misko.hevery.com/code-reviewers-guide/ (guide to writing code to test)

[Edit - in response to comment]: About what you did is correct unit testing, technically the answer is no. As I said, this is more of an integration test, which is good, but it does too much and has too many dependencies to be considered a real unit test. Ideally, a unit test will test the logic in the method, not the dependencies. The Testable Code Guide mentioned above (and the related blog Misko Hevery) gives some good advice on how to write code with seams to inject spurious dependencies. Michael Pearce explores this topic in depth in his excellent book Working Effectively with Legacy Code .

Dependency injection is key: the methods being tested do not look for their dependencies - they receive them in the constructor or as arguments. If this requires too much refactoring, there are some tricks you can try, such as moving code that looks for dependencies into protected or package-visible methods that you can override. So, you run your tests in a subclass that returns mock objects from those methods.

+4


a source


You should write your tests as early as possible, ideally before writing your implementation.

This is a book I found helpful on the subject and might be worth reading ... http://www.amazon.com/Test-Driven-Development-Addison-Wesley-Signature/dp/0321146530

To try and address the second point, what you are describing is an "integration test", meaning it is not just a test of your unit of code, but also database connectivity, configuration, data, etc.

Unit tests should only test a specific "piece" of code that you are linked to. Those. your method call should just check that method. It should not be concerned with database connectivity and data access. You can achieve this by using Mock objects to act as a temporary replacement for your dependencies.



See: http://msdn.microsoft.com/en-us/library/aa730844.aspx

As long as this document is owned by Microsoft and you are using java, the same principles apply.

Hope it helps.

+3


a source


You should design your unit tests as early as possible. In fact, the best solution would be to develop them even before you start testing the classes. Then you run them every time important functionality is added to your classes.

Unit tests, as their name suggests, are used to test a "unit" of work. You shouldn't have too many of one test. Also, the unit test should test functionality from one class.

What you are describing here is more like system testing. This is good, but it is a completely different matter than unit testing.

+2


a source


Doing certified development requires you to follow a specific devellopment model. The mostly used model is the V model, which is said to define requirements, specifications, subroutines (e.g. specifications for components, modules or blocks), etc. From top to bottom on the left branch of V. You finally get to the bottom of V, when the specifications define an atomic unit that cannot be intelligently broken down into smaller parts.

On the right-hand branch of the V-model, you check that the develloped module complies with its specifications and / or reqs. This is a typical use for creating a test in parallel or prior to implementing a UUT (device under test). Thus, you are claiming that any module fits into its structure from the very beginning and even when performing some operations.

As others have said, keep your tests as simple as possible, using mocks etc. whenever possible. You should also automate your tests, at least the ones near the bottom of the V, as these tests are usually run much more often than the ones at the top.

Getting the reports right is much easier if your tests have a well-defined outcome. We usually have an enum structure (past, failed, error) status number and message text.

0


a source


In my opinion, testing should be done from the moment you finish writing your first method.

Testing methods, when you write them, help with a lot of things. First, when something goes wrong and your only clue is that it is in a particular class, if that class has countless methods and properties, it's hard to figure out what went wrong. Obviously not if you have a syntax error, but semantic errors become very complex when you throw a million methods into the fray.

Ensuring that each individual method works that is the most granular level of confidence speaks volumes for the full class. If you have a specific definition of what a particular method should do, unit testing not only prevents semantic edge errors, but also helps you plan your program flow in front of your hand, which can help you avoid bulk refactorings or, worse, rewriting.

For effective testing, make sure you break your class into as many methods as possible. I know some might disagree with this, but a lot of the logic that performs actions in a context remote from the rest of the function needs to be moved into their own function so that it can be tested independently. I would not execute SQL queries in a unit test. Use mock objects instead to simulate a database. Database testing is not unit testing.

This will not only help you ensure the integrity of your application, but it will also help you in the future when parts of the class need to be refactored. There is no point in looking for three lines of logic in a 100-line function. If these pieces of logic are in their own method, you can be sure that your method does not fail due to this logic. If this monster of a function fails, you will have less clue as to which part of the function is actually causing the error.

I know this may seem like overkill, but my job is to test early, and testing often not only helps the stability of your program, but also helps you write clean code and avoid headaches when looking for small bugs.

0


a source


My question is when should we start testing the device?

Not "after all the code is ready to be deployed". Not "after deployment". This is too late. Like many others, the best time is (simple) before writing TDD style code.

Now here's my question ... Am I doing proper unit testing? Since one unit test method contains all the code flow and gives the result ...

You are not doing "correct" unit testing. The unit you are testing is a class method, but it also tests the database connection, which usually makes the tests too slow to run as often as we would like and fragile. This does not mean that your tests are useless - they may be useful - they just are not good unit tests.

Let's assume your database had the correct data and your program had a strong connection to it. What value will your method get from the database during your test? Just supply this value directly to test the functionality of this method. So it works even without a database. What is it? Does your method not work this way? Maybe this is too much. Maybe he (a) gets data from the database and (b) does the calculation. Maybe you really only want to test (b) (I think you do). Divide (a) and (b) into pieces to give yourself a seam (in Michael Perce's terminology) on which you can enter test data from the test method. What do you have now? Best design. Each method only does one thing. Coupling. And you got there,because your tests got you there.This is test development.

0


a source


Every time you make changes or write new code, you must include tests for each combination of inputs. Coding never ends, someone has to come back and fix or improve someday, and your tests will help them know that the changes they make have not broken anything.

0


a source







All Articles