Upgrade to Pro — share decks privately, control downloads, hide ads and more …

testing the unpredictable

testing the unpredictable

Non deterministic tests

Fran Iglesias

November 02, 2018
Tweet

More Decks by Fran Iglesias

Other Decks in Programming

Transcript

  1. Testing the unpredictable Elements beyond our system: • Randomness •

    Time • File system You can’t rely on their behavior
  2. 1. Things that are using unpredictable components They are global

    state dependencies Isolate and extract Invert dependency Stub for testing
  3. Example class User { private $id; private $name; public function

    __construct(string $name) { $this->id = Uuid::uuid4(); $this->name = $name; } }
  4. Example class User { private $id; private $name; public function

    __construct( UserId $id, string $name ) { $this->id = $id; $this->name = $name; } }
  5. 2. Things that are unpredictable Don’t test what you don’t

    own Test for properties of the output, not the output itself
  6. Example class PasswordGeneratorTest extends TestCase { public function testShouldBeLongEnough() {

    $password = $this->generator->generate(); $this->assertTrue(strlen($password) > 9); } }
  7. Dependency Inversion High level modules should not depend on low

    level modules, both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.