Giter Club home page Giter Club logo

Comments (4)

jakoch avatar jakoch commented on July 19, 2024 1

Hello!

I thought I remember reading that SimpleTest can do Integration tests and not only Unit tests..

  • Only assert for the current test case in the current context.
  • Do not pass assertion parameters through UnitTestCase context boundaries.
  • UnitTests cover the functional behavior of each classes to integrate (standalone).
  • IntegrationTests cover the combined behavior of the classes (combinatorical).

The following example unit-tests A, B and C,
then finally we do an integration test for C using A and B.

<?php

require_once __DIR__ . '/../../autorun.php';
require_once __DIR__ . '/../../test_case.php';

class A { function fA() { return 'Hello '; } }
class B { function fB() { return 'World!'; } }
class C { function fC($a, $b) { return $a . $b; } }

class aTest extends UnitTestCase 
{
    function setUp()
    {
        $this->A = new A();
    }

    // unit test for class A - function fA
    function testAfA() 
    {
        $this->assertEqual('Hello ', $this->A->fA());
    }
}

class bTest extends UnitTestCase 
{
    function setUp()
    {
        $this->B = new B();
    }

    // unit test for class B - function fB
    function testBfB() 
    {
        $this->assertEqual('World!', $this->B->fB());
    }
}

class cTest extends UnitTestCase 
{
    function setUp()
    {
        $this->C = new C();
    }

    // unit test for class C - function fC
    function testCfC()
    {
        $this->assertEqual('12', $this->C->fC('1', '2'));
        $this->assertEqual('NY', $this->C->fC('N', 'Y'));
    }
}

class cIntegrationTest extends UnitTestCase 
{
    function setUp()
    {
        // we want to test that C works with the following dependencies:
        $this->A = new A();
        $this->B = new B();

        // the subject under test for integration is C
        $this->C = new C();
    }

    function testC_Integrates_With_AB()
    {
        $this->assertEqual('Hello World!', $this->C->fC($this->A->fA(), $this->B->fB()));
    }
}


Why do you get the Fatal error: Call to a member function getDumper()?

  • (testStart has no assertion, but instantiates fooTest; it should probably be a setUp() method instead)
  • bazTest extends UnitTestCase. that's the running test, which has an Reporter object internally.
  • but testDo calls foo->doFoo() and leaves the context of the currently running test bazTest
  • the assertTrue() in fooTest->doFoo doesn't count for the bazTest, because $this means fooTest, which is another UnitTestCase. that one is currently not being run and has no reporter internally.
  • in the end it fails, because it can't get the dumper from the reporter

Well, to satisfy you could technically pass the reporter forward - but that creates a real mess.
Don't use this approach! It's just to understand what "context of the running test" means.

<?php

require_once __DIR__ . '/../../autorun.php';
require_once __DIR__ . '/../../test_case.php';

class bazTest extends UnitTestCase 
{
    function setUp()
    {
        $this->foo = new fooTest();

        // copy the reporter context into the foo object
        $this->foo->reporter = $this->reporter;
    }

    function testDo() 
    {
        $this->foo->doFoo(false);
    }
}

class fooTest extends UnitTestCase
{
    function doFoo($foo) 
    {        
        $this->assertTrue($foo);    
    }
}

Regards, Jens

from simpletest.

aland avatar aland commented on July 19, 2024 1

Sorry, didn't mean to be rude. I saw you closed the ticket already and thought I had to reopen the ticket to comment (maybe I was logged out?).

I really appreciate the detailed response, it helps a lot. I've come up with something which works for me now. Rather than have multiple classess extend UnitTestCase - I have separate classes (componentTestA, .. ) which integrate components, these classes are initialised in the setUp method.

class componentTestA { }
class componentTestB { }
abstract class TestCase extends UnitTestCase {
    function setUp() {
        $this->a = new componentTestA();
        $this->b = componentTestB();
    }
    function assertStuffWithA() {
        $this->assertTrue($a->isTrue());
    }
    function assertStuffWithB() {    }
}

class TestingStuff extends TestCase {
    function testAB() {
        $this->assertStuffWithA();
        $this->assertStuffWithB();
    }
}

I'm happy how it's working for me now, my test methods are clear and readable and testCase has simple interactions with components. A minor gripe is that TestCase is getting long in the tooth and while testX methods assert across multiple componentTests, the individual assertStuffWithX methods only use a single componentTest.
On positive side while writing the aTest classes I'm getting idea's how to refactor the underlying classes.

I did try the workaround you suggested, it worked but I resisted the temptation to leave it in and carry on as I was.

from simpletest.

jakoch avatar jakoch commented on July 19, 2024

i appreciate your feedback

from simpletest.

jakoch avatar jakoch commented on July 19, 2024

Of course, you can always create an AbstractTestBase class. lgtm

from simpletest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.