Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7070015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:30:54+00:00 2026-05-28T05:30:54+00:00

Currently, I’m trying to apply unit testing to a project for the first time.

  • 0

Currently, I’m trying to apply unit testing to a project for the first time. Two questions arose:

  1. Is it bad practice if multiple tests depend on each other? In the code below, several test need the outcome of the other tests to be positive, is this general best practice?

  2. How far do you go with mocking objects the SUT depends on? In the code below, the ‘Router’ depends on ‘Route’, which depends on ‘RouteParameter’. To mock, or not to mock?

The code below is to test my ‘Router’ object, which accepts routes via Router::addRoute($route) and routes an URL via Router::route($url).

class RouterTest extends PHPUnit_Framework_TestCase {
    protected function createSimpleRoute() {
        $route = new \TNT\Core\Models\Route();
        $route->alias = 'alias';
        $route->route = 'route';
        $route->parameters = array();

        return $route;
    }

    protected function createAlphanumericRoute() {
        $route = new \TNT\Core\Models\Route();
        $route->alias = 'alias';
        $route->route = 'test/[id]-[name]';

        $parameterId = new \TNT\Core\Models\RouteParameter();
        $parameterId->alias = 'id';
        $parameterId->expression = '[0-9]+';

        $parameterName = new \TNT\Core\Models\RouteParameter();
        $parameterName->alias = 'name';
        $parameterName->expression = '[a-zA-Z0-9-]+';

        $route->parameters = array($parameterId, $parameterName);

        return $route;
    }

    public function testFilledAfterAdd() {
        $router = new \TNT\Core\Helpers\Router();

        $router->addRoute($this->createSimpleRoute());

        $routes = $router->getAllRoutes();

        $this->assertEquals(count($routes), 1);

        $this->assertEquals($routes[0], $this->createSimpleRoute());

        return $router;
    }

    /**
    * @depends testFilledAfterAdd
    */
    public function testOverwriteExistingRoute($router) {
        $router->addRoute(clone $this->createSimpleRoute());

        $this->assertEquals(count($router->getAllRoutes()), 1);
    }

    /**
    * @depends testFilledAfterAdd
    */
    public function testSimpleRouting($router) {
        $this->assertEquals($router->route('route'), $this->createSimpleRoute());
    }

    /**
    * @depends testFilledAfterAdd
    */
    public function testAlphanumericRouting($router) {
        $router->addRoute($this->createAlphanumericRoute());

        $found = $router->route('test/123-Blaat-and-Blaat');

        $data = array('id' => 123, 'name' => 'Blaat-and-Blaat');

        $this->assertEquals($found->data, $data);
    }

    /**
    * @expectedException TNT\Core\Exceptions\RouteNotFoundException
    */
    public function testNonExistingRoute() {
        $router = new \TNT\Core\Helpers\Router();

        $router->route('not_a_route');
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:30:54+00:00Added an answer on May 28, 2026 at 5:30 am

    1) Yes it is definitely a bad practice if tests depend on each other.

    A Unit Test should be constructed in such a way that when it fails it immediately points to a specific area in your code. Good Unit Tests will reduce the time you spend debugging. If Tests depend on each other you will loose this benefit because you can’t tell which error in your code made the test fail. Also, it’s a maintenance nightmare. What if something changes in your ‘shared test’, then you would have to change all depended tests.

    Here you can find some good guidance on how to solve the interacting test problems (The whole xUnit Test pattern book is a must read!)

    2) Unit Testing is about testing the smallest thing possible.

    Let’s say you have an alarm clock (C# code):

    public class AlarmClock
    {
        public AlarmClock()
        {
            SatelliteSyncService = new SatelliteSyncService();
            HardwareClient = new HardwareClient();
        }
    
        public void Execute()
        {
            HardwareClient.DisplayTime = SatelliteSyncService.GetTime();
    
            // Check for active alarms 
            // ....
        }
    }
    

    This is not testable. You will need a real satellite connection and a hardware client to check if the right time is set.

    The following however will let you mock both hardwareClient and satelliteSyncService.

    public AlarmClock(IHardwareClient hardwareClient, ISatelliteSyncService satelliteSyncService)
    {
        SatelliteSyncService = satelliteSyncService;
        HardwareClient = hardwareClient;
    }
    

    You should however never mock an object that you are actually testing (sounds logical but sometimes I see it happening).

    So, how far should you go with mocking. You should mock everything that the class your testing depends on. In such a way you can test your class in complete isolation. And you can control the outcome of your dependencies so you can make sure that your SUT will go trough all code paths.

    For example, let SatelliteSyncService throw an exception, let it return an invalid time, and off course let it return the correct time and then at specific moments so you can test if your alarm is activated on the right moment.

    For creating your Route test data. Consider using the Builder Pattern. That will help you in only setting what is required for your test to succeed. It will make your tests more expressive and easier to read for others. It will also lower your test maintenance because you have fewer dependencies.

    I wrote a blog post about unit testing that expands on the ideas mentioned here. It uses C# to explain the concepts but it applies to all languages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have multiple resources in a project that can only be accessed from
Currently we have a project with a standard subversion repository layout of: ./trunk ./branches
Currently I'm doing some unit tests which are executed from bash. Unit tests are
Currently, I am writing up a bit of a product-based CMS as my first
Currently I know of only two ways to cache data (I use PHP but
Currently i'm having trouble using two functions in my -(void)viewDidLoad , both of these
Currently I'm trying to parse some html and return an array with the values
Currently if i have dependency project opened, then maven use it instead of specified
Currently I'm learning C++ and I've been trying to create a simple image processing
Currently working on database part of android project. The main aim of the project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.