Skip to content

Feature : automatic unit test generation based from contracts #36

Open
@lcetinsoy

Description

@lcetinsoy

Hello !

It is me again haha. Imagine the following : you put some CDD assertions with phpdeal (or other tool) and you have some automatic unit tests generated automatically. Wouldn't that be so cool ?.

I got this idea from the Hoa\Praspel project which is a specification language usable for CDD. It is similar to phpdeal, albeit a bit more potent maybe. They developed a small plugin which generates automatic unit test from Praspel contracts into atoum cf link. Unfortunately this idea was not pushed more and the main author of Hoa stopped to work on it.

I find this idea fascinating with a lot of potential and I may try to implement it for phpunit. I wanted to have your thoughts about it to get feedbacks. I am considering two options :

Idea 1 : Simply port hoaproject/Contributions-atoum-praspelExtension to phpunit since atoum ... well was not successful

Pros :

  • No too difficult I would say for a first shot.
  • Praspel comptability with Realistic domain can let specify very precise contracts

Cons :

  • They do not have a CDD implementation like phpdeal which are useful to catch errors at system level.
  • No integration with phpdeal which "has more user" than Praspel.

Idea 2 : Build the same mechanism directly from phpdeal. Maybe we could integration RealDom into it

Pros :

  • You have CDD and PhpUnit automated tests
  • Integration with Praspel\RealisticDom or other things (like Faker ) could be added to enrich assertions

Cons :

  • The mechanism to sample inputs from Verify contracts would be complicated to build ? I do not know yet

So here are my questions :

  1. What are your thoughts about this idea, overall ?
  2. Would some people be interested to help if I started to build something ?
  3. Would this built as a separated repository or integrated in this one ? I guess Separated.
  4. Regarding implementation of idea 2, do you agree with the following ?

A generated test would do the following :
1. Samples inputs according the Verify contracts
2. Run phpunit assertion using Ensure contracts.
3. Also load phpdeal "normally" to have invariants contract checking for free during tests.

To do that, the following could be done :

Load a php file to test and for each public method:

  1. Generate input data
       1.1 Read Verify contracts with MethodConditionFetch class ?
       1.2 Sample input from it with some Sampler classes to be written.
       1.3 Generate code for input data
  2. Generate unit test assertion
       1.1 Read Ensure contracts with ??
       1.2 For each Ensure contract generate a test assertion.
  3. Generate the whole test code in phpunit (or other framework) with a class to be written.

As I write and specify the to-do list, I get it could be a bit difficult (how to construct the object to be tested automatically if it is complex). I will keep updating, the post as I progress in my thinking.

What do you think ?

edit : eris and property based testing are also relevant to the thinking

Best

Laurent

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions