How do you test your code?

Hi All,

Being a single developer, I rarely documenting or planning my testing.Although I did test my code, but just test that it works. I know this is bad. I think Yii provides good test framework. My question is how do you do your testing? Will you do the Functional tests only? Or will you do the model testing? If it is just a simple Customer model CRUD pages. How do you test?

I want to start developing by testing first. Please give me some guidance.

Thank you,

Daniel

I test complicated things with unit testing and simple stuff like CRUD either with Codeception or… manually.

@samdark thanks for the sharing. May I ask you further, if simple CRUD generated by yii. For example, customer database. No complicated rules or logic. Just validation rules,

  1. like code and name should be unique,
  2. Code can be generated automatically for next code (get max value +1) if we use XXX at the end of the code.

Do we need to test use codecept? Or just manual test? I usually screen record the manual test sine I do not have time to write test document.

I am sorry to ask these questions. I want to learn from the expert on how they are doing their work.

I use yii from v1.0. Even I knew yii before php. On my uni, I only learn java. I learned php through yii. I love this framework.

Depends if it’s admin panel or user-available website. If it is admin panel and something not working doesn’t cost much, I’d not test it. Will be reported by admin. If it’s user-oriented then Codeception would do.

Manual tests are rarely a good thing, since you can’t really tests all the situations, and, worst part is, you’re the developer so you know the product and may end up unconsciously only test the path that work. You will also get sloppy with time, and forget to tests things.

A good way to start with testing, if you’re using basic or advanced templates, is to use the tests already provided as a basis.

Then, every time a bug is reported/found, try to write a test that reproduces it and verify that it’s fixed. With time your tests suites will grow and so will the quality of your product.

I want to start developing by testing first.

Not sure what you mean, but if you’re referring the TDD approach (Test Driven Development), I wouldn’t personally start with it. That’s something to revisit once you’re more confident around automated tests, and would better be tried on a new project.

1 Like

@machour
thank you for providing good guidance. I agree with you that manual test lead me to the mistakes since I am the developer and becoming subjective. Retest means run it manually again.

I ever read about selenium to automate testing. Any guidance to test yii with selenium or similar application?

Does codecept only deals with console? not gui?

I generally start form outside in test high level stuff with codeception, although lately I have been using a set of helpers with phpunit very similar to codeceptions but minimal class, then If I have some complicated logic I write unit tests for it i hardly write integrations test I do that if I really need to.

Any guidance to test yii with selenium or similar application?

https://codeception.com/for/yii#acceptance-tests

I’m not aware of a GUI, but why would you need it?

I’m not aware of a GUI, but why would you need it?

I am not sure. I should do the reading on your guidance before comments. However, my current idea is that codecept is more like testing in console way, while I think something like selenium (I am not sure either) is GUI way of testing since we record our test and can play it back later to redo the test.

Even with Selenium you won’t see a thing :slight_smile:
I definitely recommend going through the resources I have you and start seeing concrete things at work, it will make them clearer for you.

@machour
I am so sorry, I do not know selenium well. I was thinking that with selenium we used GUI. Probably, I was thinking more to how we made the test case. Recorded in GUI then playback. I am so sorry if I am wrong.

Thank you all for the guidance and elaborations. First thing to do from this is study the tests provided.

That depends. (It always depends.) You may often encounter sweeping or general statements of the virtues of automatic testing and automated test cases. But specific context matters too and sometimes the general statements can be unhelpful in your specific situation.

As Pragmatic Dave Thomas put it:

Don’t let the turkey’s get you down!

In which turkeys are experts that tell you want to do.

Testing can be extremely valuable if the test cases are good and if the costs are within your budget. For a large project with many developers there’s a lot of experience to show that investment in testing from the start can save time and resources in the long run, assuming the testing is competently executed. Hence allocating a big budget for test can make sense. It depends.

But if you are on your own then you aren’t developing anything very complex. The return on investment in testing might be much smaller. Perhaps so small that you only test certain parts. It depends.

So the value of tests depends on a lot of things.

My own answer to your question, “How do you test?” is similar to @samdark. We don’t do TDD. We use unit tests for difficult bits of code. 95% of the functionality in our product is admin panel and, for the most part, we test changes manually and rely on users and Rollbar to report regressions and bugs.

OT and BTW, isn’t it funny that there’s a fashionable coder conference with a title that celebrates two of the worst mistakes in programming language design? All that and it’s strlen() is only 5!

1 Like

@thefsb @samdark @machour @alirz23

Thank you for the explanation. What I got from all of your generous guidances:

  1. Testing is as valuable as the good test case and the cost is within the budget.
  2. Yii2 has example of codecept test that I can start to look at to do the testing.

Telling you the truth, I am asking this question because I am starting a new career and looking for a job right now.

While I had many projects in yii2 and I tested them all (otherwise my clients will complain me if not working). I do not know how to show them that I tested my codes. I am afraid that the interviewer will think that I am not doing the testing.

An indicator of testing is usually code coverage metric. The more code is covered the more stable is the project.

Please give some feedback,

Say, i have a customer model + CRUD. The actions are basically just the CRUD output. I used kartik-v extension and ajax crud. my testing would be acceptance test which cover,

  • create a new customer
    the scenario is create a new customer, check if its appeared in gridview
  • update customer data
    choose a customer, update the phone number, check the updated phone number in gridview.
    also test on modifying the customer code to be the same of other existing customer. it should displayed error message, “Customer code is already taken.” or “Customer code must be unique”.
  • delete user
    delete a user and check if it is no longer appeared in gridview.

Is that enough? beside customer code must be unique, I have other validation rules, like some fields are required. name must not be longer than 20 characters, status can only be 0, Inactive and 1, Active.

I just thinking that I probably need around 30 minutes to write the test and run it. Do I need them to be tested as well? However, I think I would be better spending 30 minutes to do other models than testing this. To me they are very general and trivial. But again, if not done, will recruiter think I am lazy and my code would be regarded as low quality?

The test set you’ve mentioned would be a good start but overall covering CRUD isn’t needed mostly. It’s too simple.

Validation is a bit more practical since mistakes are more likely.

If you have complicated logic, start with it instead.