Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing: Beginner Questions

I'm finally starting out with unit testing, having known that I should be doing it for a while, but I have a few questions:

  • Should or shouldn't I retest parent classes when testing the children if no methods have been overwritten?
  • Conceptually, how do you test the submitted part of a form? I'm using PHP. (Edit: The reason I ask this is that I have a high level form class that generates a form, validates it, filters it, and generates any error messages by taking a JSON-like array as input and delegating to a variety of smaller classes. However, I can't test the errors, etc without submitting the form. Edit: This looks like it might be an answer.)
  • If you have optional parameter in a method, should you write a test for both when they are present and when they are not?
  • Should unit testing in any way be combined with testing code execution time or should they remain completely separate?
  • Is there any valid reason not to run your full test suite every time?
  • Just so I'm getting my terminology right, to what does the unit in unit testing refer? The class being tested? The method? The parameter? Something else?
like image 620
VirtuosiMedia Avatar asked Jan 23 '09 16:01

VirtuosiMedia


People also ask

How do you write a unit test for beginners?

Unit testing follows a pretty common strategy - AAA, or Arrange, Act, Assert. First, arrange all preconditions for your test to run. Next, Act or execute your code. Lastly, assert the correct things happened.

What is basic unit testing?

A typical unit test contains 3 phases: First, it initializes a small piece of an application it wants to test (also known as the system under test, or SUT), then it applies some stimulus to the system under test (usually by calling a method on it), and finally, it observes the resulting behavior.

Is unit testing easy to learn?

It's not hard to learn unit testing: The hard part is to make sure you write tests for all the possible scenarios, which will give you 100% confidence in your deployed codebase. The hard part comes when the existing code is not suitable for covering with unit testing. That kind of code is called legacy code.


1 Answers

  • Test parent, test child; if the child doesn't override the parent method, no need to retest it.
  • I'm not sure I understand the second one. You can use Selenium to automate testing a form. Is that what you mean?
  • Tests should include the "happy path" and all edge cases. If you have an optional parameter, write tests to show proper operation with the value present and absent.
  • Unit tests, integration tests, acceptance tests, load tests are all different ideas that may have some overlap.
  • I'll bet there are valid reasons, but if you're doing automated builds that run the test suite automatically why would you not run them? Maybe long run times come to mind, but that's the only reason I can think of. The value is seeing that all of them continue to pass, and that changes you've made haven't broken anything.
  • Unit test to me means the class you're testing, which can have several methods. I associate them with classes, not forms. Forms mean UI and integration testing to me.
like image 126
duffymo Avatar answered Sep 19 '22 16:09

duffymo