Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JenuelDev's avatar

Laravel Test How To Modify Output

Hi! I am wondering how to change the output of unit test. Currently when testing the output will be based on the function name.

image

This is really helpful, specially with long sentence,

this is my sample test function. My Goal here is to output something like this The Date is 2023-11-07 day to bill is 15 with a price of 50, expects output to be 13.15 instead of just outputing the function name cause dots cant be added in a function name, and commas.

public function testDayToDay2023_11_07DayToBillIs15PriceIs50($dayToBill = 15, $dayToday = '2023-11-07', $price =
    50) {
        // Getting the Pro Rata Charge
        $proRataFrom = (new DateTime($dayToday))->format('Y-m-d');
        $proRataTo = ProRataCalculator::calculateTheDateOfProRataTo($dayToBill, $proRataFrom);

        $proRataCharge = AccountBalanceLedger::calculateFee(
            $price,
            $proRataFrom,
            $proRataTo
        );

        $proRataCharge = (float)number_format($proRataCharge, 2, '.', '');

        $this->assertTrue($proRataCharge == 13.15, 'Expected 13.15, got ' . $proRataCharge);
    }
0 likes
4 replies
tykus's avatar
tykus
Best Answer
Level 104

Use the @testdox annotation to write the output you want

https://docs.phpunit.de/en/9.6/annotations.html#testdox

/**
 * @testdox The Date is 2023-11-07 day to bill is 15 with a price of 50, expects output to be 13.15
 */

You can use the method's parameter names in the annotation to dynamically substitute values into the output.

Aside, why is your test method accepting parameters if you are not using a data provider???

1 like
JenuelDev's avatar

@tykus thanks a lot iLl try this one and will come back to you.. for the parameter,, I just put it there,, Im going to change it to a variable something like this:

public function testDayToDay2023_11_07DayToBillIs15PriceIs50() {
		$dayToBill = 15;
		$dayToday = '2023-11-07';
		$price = 50;

        // Getting the Pro Rata Charge
        $proRataFrom = (new DateTime($dayToday))->format('Y-m-d');
        $proRataTo = ProRataCalculator::calculateTheDateOfProRataTo($dayToBill, $proRataFrom);

        $proRataCharge = AccountBalanceLedger::calculateFee(
            $price,
            $proRataFrom,
            $proRataTo
        );

        $proRataCharge = (float)number_format($proRataCharge, 2, '.', '');

        $this->assertTrue($proRataCharge == 13.15, 'Expected 13.15, got ' . $proRataCharge);
    }

its working now, running test with --testdox did the trick.

tykus's avatar

@BroJenuel glad to help.

If I was reviewing a PR with that test, I would be wondering about the specificity of method naming, and again about the use of parameters when there is no opportunity to change them (e.g. using a data provider)

1 like
JenuelDev's avatar

@tykus the test is not final yet, Im still working on the output... so this is what I came up with:

/**
     * @testdox The Date is 2023-11-07, Day to bill is 15 with a price of 50, expects output to be 13.15
     * @throws Exception
     */
    public function testProrataChargeTestOne()
    {
        $dayToBill = 15;
        $dayToday = '2023-11-07';
        $price = 50;
        $expectedOutput = 12.90;

        // Getting the Pro Rata Charge
        $proRataFrom = (new DateTime($dayToday))->format('Y-m-d');
        $proRataTo = ProRataCalculator::calculateTheDateOfProRataTo($dayToBill, $proRataFrom);

        $proRataCharge = AccountBalanceLedger::calculateFee(
            $price,
            $proRataFrom,
            $proRataTo
        );

        $proRataCharge = (float)number_format($proRataCharge, 2, '.', '');
        $this->assertEquals(
            $expectedOutput,
            $proRataCharge,
            "Expected $expectedOutput, got $proRataCharge ($proRataFrom, $proRataTo)"
        );
    }

Please or to participate in this conversation.