samedi 22 août 2020

Feature tests that depend on tools that are being tested?

I'm learning how to write feature tests.

I've been assigned a project built with Laravel PHP Framework. I see that my team uses the app/ folder to organize all the usual things like models, data transfer objects, controllers, etc... basically all the application logic for the REST API this project is responsible for.

I also noticed a folder called tests/feature that contains what I recently learnt are feature tests. What I found interesting was code that looked something like this:

use App\Profile;

class HobbyFeatureTest extends FeatureTestCase
{
    // other code ...etc...
    public function testGetUserHobbyyOnUserWithNoHobby()
    {
        $response = $this->getJson('api/user/me/hobbies')->assertOk();  /// makes a PHP curl call
        $response->assertJson($this->profile->hobby->toArray());
    }
    // other code ...etc...
}

Basically $this->profile is an instance of App\Profile. And api/user/me/hobbies also depends on App\Profile in its construction.

I found this unusual because this means you're testing with the tools that are being tested? Isn't that a problem?


BACKGROUND TO MY QUESTION

I just realized that a few years ago, I wrote feature tests without knowing what they were really called. What I did differently was I wrote all my feature tests in ruby language in a completely separate code base (git project) to test a restful API that was built in golang. My ruby code would load mock database snapshots, then ping the golang api and evaluate their responses. My motivation for having two code bases were:

  1. I wanted the underlying code for my feature tests to be as exclusive as possible from the code being tested. I was trying to implement the analog to double-entry bookkeeping in accounting.
  2. I wanted my feature tests to be re-useable if I re-platform my API to a different technology. This proved useful because the API was originally built with PHP5. When PHP5 reached end-of-life, I rebuild the API with golang under a new architecture (needed address some technical debt as well). I was please to be able to retain and re-use 90% of my ruby feature tests.

Based on my only other experience writing feature tests, it struck me as odd that Laravel took an approach that is contradicts the two benefits I realized from my own approach to feature tests with ruby. Hence, I was curious of the benefits of testing with tools that are to be tested itself?

Aucun commentaire:

Enregistrer un commentaire