Ensuring that there are tests for a given set of classes (static analysis?)

Published 1 week ago by bensampo

I'm building an API using Laravel's resources. I want to make sure that the output of each one conforms to my expectations, so for each resource class I have a corresponding unit test.

What I'd ideally like to do is have a test which grabs a list of all of my resource classes and makes sure there is a corresponding unit test.

Essentially I want to test that there are tests.

Is there a sensible/standard way to do this?

Best Answer (As Selected By bensampo)
click

I solved it once by just looping over my 'classes to be tested' and looked if there where matching test classes which had a suffix 'Test'. And the same for the methods inside it.

And that was one of the first tests that was ran. Not an ideal solution in all situations but for my case it worked well.

martinbean

@bensampo If you want to check the output of your API adheres to a specification, then there are standards to assist in this. Look up “OpenAPI” and JSON schema.

bensampo

Thanks @martinbean, to clarify: I want to check that the output contains the data I expect, rather than that it conforms to a standard.

click
click
1 week ago (59,590 XP)

I solved it once by just looping over my 'classes to be tested' and looked if there where matching test classes which had a suffix 'Test'. And the same for the methods inside it.

And that was one of the first tests that was ran. Not an ideal solution in all situations but for my case it worked well.

ohffs
ohffs
1 week ago (248,410 XP)

This package from Spatie might be of help for the content/data checking?

https://github.com/spatie/phpunit-snapshot-assertions

To ensure there are tests for each resource is a bit of a rabbit-hole I think. You could write a script or artisan command which looped over all of the files in app/Http//Resources/ and complained (or created) a missing matching test in tests/Api/Resources/ or something - but then you need to remember to run it, so you need a test to test that you've run your script to make your tests ;-)

Or I guess you could add an artisan command like 'make:tested-resource' which itself called 'make:resource' and 'make:test' - but then you need to remember to run that rather than 'make:resource', so you're back to just having to remember to do it :-)

click
click
1 week ago (59,590 XP)

@ohffs you could just solve it by making the test to validate that there are tests part of your test suite. That solves the problem: "but then you need to remember to run it"

bensampo

@click This is pretty much what I have ended up with. I have a test which loops over all of the files in the resource folder and makes sure that a corresponding test files exists.

Thanks for the suggestions all!

JarekTkaczyk

your/app/path$ vendor/bin/phpunit --coverage-html=coverage && sensible-browser coverage/index.html (with xdebug enabled) and you're good

Please sign in or create an account to participate in this conversation.