SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Non-Member
    Join Date
    Sep 2004
    Location
    Florida
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question unit testing - what is wrong?

    hello, just starting to learn unit testing with simple test and not getting off to a good start.

    can someone tell me where im going wrong please?

    PHP Code:
    class authenticator {
            private 
    $connection;
            
            public function 
    __construct() { 
            
            }
            
            public function 
    is_logged$username$password ) {
                if( 
    $username == 'user' && $password == 'pass' ) {
                    return 
    true;
                } else {
                    return 
    false;
                }
            }
                }

    class 
    unit_test extends UnitTestCase {
            public function 
    __construct() {
                
    $this -> UnitTestCase();
            }
            
            public function 
    first_test() {
                
    $auth = new authenticator;
                
    $this -> assertTrue$auth -> is_logged'test''pass' ) );
            }
        }

        
    $test = new unit_test;
        
    $test -> run( new HtmlReporter() ); 
    going by the above i should get a 'fail' box but no matter what i try i only ever get the 'pass' box

  2. #2
    SitePoint Enthusiast Refresh's Avatar
    Join Date
    Jul 2004
    Location
    Lausanne, Switzerland
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am no expert in unit testing but I expect that
    $this -> assertTrue( $auth -> is_logged( 'test', 'pass' ) );
    is causing the trouble. In fact I *think* that assertTrue needs two arguments, and it compares them.
    Maybe change it to
    $this -> assertTrue( $auth -> is_logged( 'test', 'pass' ), true );
    and see what happens...

  3. #3
    Non-Member
    Join Date
    Sep 2004
    Location
    Florida
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your help but that isnt the problem either, taking a look at this example from www.lastcraft.com

    PHP Code:
    <?php
    require_once('simpletest/unit_tester.php');
    require_once(
    'simpletest/reporter.php');
    require_once(
    '../classes/log.php');

    class 
    TestOfLogging extends UnitTestCase {
        
        function 
    testCreatingNewFile() {
            @
    unlink('/temp/test.log');
            
    $log = new Log('/temp/test.log');
            
    $this->assertFalse(file_exists('/temp/test.log'));
            
    $log->message('Should write this to a file');
            
    $this->assertTrue(file_exists('/temp/test.log'));
        }
    }

    $test = &new TestOfLogging();
    $test->run(new HtmlReporter());
    ?>;
    not sure but is because im using PHP5?

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2004
    Location
    netherlands
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use the word test before your test method. So: testFirst()

    As stated on lastcraft.com:
    "Actual tests are added as methods in the test case whose names by default start with the string "test" and when the test case is invoked all such methods are run in the order that PHP introspection finds them"

  5. #5
    Non-Member
    Join Date
    Sep 2004
    Location
    Florida
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a lot, now i get an 'fail' box

    btw which part of the site did you find the above?

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2004
    Location
    netherlands
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Under simpletest->unit tester->3rd paragraph

  7. #7
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    change
    PHP Code:
    public function first_test() { 
    to something like
    PHP Code:
     public function test_is_logged() { 
    IIRC, only methods that start with "test" are actually used in the test case.

    Also you could do
    PHP Code:
    $this->dump('I am here'); 
    in a test method to verify that it is being run.

    HTH
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  8. #8
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Refresh
    In fact I *think* that assertTrue needs two arguments, and it compares them.
    AssertTrue does take two arguments, but the second is an option message to log. Perhaps you are thinking of AssertEqual, which compares the first two arguments and optionally accepts a message for a third argument.

  9. #9
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by KillBill3
    thanks a lot, now i get an 'fail' box

    btw which part of the site did you find the above?
    Interesting. I am so used to looking at test cases now that I just expect to see the word test at the beginning of test methods and so didn't emphasize it enough. It is of course completely counterintuitive if you are used to normal classes. Why should only certain methods be run after all? I'll try to mention it more than once in the documentation so as to drip drip it into people's brains.

    The reason b.t.w. is so that you can put common behaviour into the test cases and not have it run by accident...
    PHP Code:
    class TestOfMyLovelyStuff extends UnitTestCase {

        function 
    testMyStuff() {
            
    $this->prepareTheTest($with_this);
            ...
        }

        function 
    prepareTheTest($with_this) {
            ...
        }

    This behaviour comes from JUnit. In fact JUnit even reinstantiates the test case on each call to test*. That was a bit too confusing and a bit inefficient, so the test case in SimpleTest is instantiated on first use (like NUnit).

    People are starting to look again at the JUnit interface, classic that it was. JBehave creates a duplicate of the class under test and you set test methods with the prefix "should". It makes the wording of the tests more natural. You can have shouldNotCrashWhenWeDoThis() for example. Actually, SimpleTest can do that to. I am writing the docs. for it now.

    yours, Marcus

    p.s. You don't need an explicit constructor in the test cases anymore. The documentation is only now catching up with this.
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  10. #10
    Non-Member
    Join Date
    Sep 2004
    Location
    Florida
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a lot everyone im just learning, is there more examples that i could look at?

  11. #11
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by KillBill3
    thanks a lot everyone im just learning, is there more examples that i could look at?
    SimpleTest itself has tests. Alternatively you can search for projects tested with PHPUnit or SimpleTest or even JUnit if you are familiar with Java.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •