Test cases for an application which has both php and javascript

后端 未结 1 799
不思量自难忘°
不思量自难忘° 2021-01-28 12:31

There is an application which uses both php and javascript. If I want to write test cases for this application. Shall I have to write both (phpunit test cases for php code and j

相关标签:
1条回答
  • 2021-01-28 13:06

    The first important concept is: to be tested, your code must be run, and for that you have to use the environment needed for each case:

    • for php: usually the php command line executable
    • for Javascript: a real browser, a headless browser like PhantomJS, or node.js

    Once you have this clear, it's easy to understand how to setup the tests. Use PHPUnit for testing your php files and classes, and your Javascript framework of choice for testing Javascript code.

    Regarding Javascript code being mixed with php code:

    • Strictly, the Javascript code will finally be sent to the browser, even if it is being written by php. So it should be testable running it in a Javascript environment.

    • But in reality, this probably is an indication of a poorly structured app, in which Javascript code is not separated from the backend code, and probably does not use objects and separation of concerns. This will make the code quite hard to test.

    IMO the hardest part of testing is being able to write testable code, not writing the actual tests. If your code does not uses objects, some kind of dependency injection, templates for writing the HTML and the Javascript code is not fully separated from the backend code, I'd say you'll have a bad time trying to test it. My advice is that you rewrite some part of your app using best practices and try to test it. Using a framework usually helps because frameworks enforce the separation of concerns and usually have unit testing integrated.

    0 讨论(0)
提交回复
热议问题