Skip to content

Magento 2 and TDD good practices to implement tests

here is my concern, when it comes to testing in Magento 2, I find documentation is quite obselete or very difficult to understand.

If PhpUnit offers ways to test a simple method…I feel like as soon as Magento 2 objects and database are getting involved, everything is getting super duper complicated. So here I am looking for real and working examples about testing, rather we use it for TDD or not, I think test should take a more important part on our development process.

So, here are some use cases I would like to ensure threw working coding examples. I hope some of you may have leads about it.

Use Case number 1 :

Get a product / Change his price / Save the product

Check the expected price has been properly saved.

Use Case number 2 :

Create a product Given a free SKU

Check that the expected product has been properly created.

Use Case number 3 :

Create a product Given a taken SKU

Check that the product can’t be created as the sku is already taken

I’m sorry I can’t provide any code for this I just have no idea how to start about it. I feel like there might be a data fixture implied somehow…but really no idea…I would appreciate any working exemple in any of these use cases or similar with custom entities.