同时具有php和javascript的应用程序的测试用例 [英] Test cases for an application which has both php and javascript

查看:74
本文介绍了同时具有php和javascript的应用程序的测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个同时使用php和javascript的应用程序.如果我要为此应用程序编写测试用例.我是否必须同时编写两个代码(用于php代码的phpunit测试用例和用于javascript代码的jasmine测试用例)?

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 jasmine test cases for javascript code)? .

我已经从链接https://phar.phpunit.de/phpunit.phar

我只是不清楚如何开始测试用例.请帮助. 从链接 https://softwareengineering.stackexchange.com/questions/28306/can-i-test-effectively-javascript-functions-with-a-php-unit-testing-framework ,他们说我必须为javascript编写javascript测试用例代码和php代码的php测试用例.但是我的JavaScript代码介于php代码之间.如何编写和执行测试用例?

I am just not clear how to start the test cases.Please help . From the link https://softwareengineering.stackexchange.com/questions/28306/can-i-test-effectively-javascript-functions-with-a-php-unit-testing-framework , they say I have to write javascript test cases for javascript code and php test cases for php code. But my javascript code is in between php code. How can I write and execute the test cases?

推荐答案

第一个重要概念是:要进行测试,必须运行您的代码,为此,您必须使用每种情况所需的环境:

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:

  • 对于php:通常是php命令行可执行文件
  • 对于Javascript:真实的浏览器,无头浏览器(如PhantomJS或node.js)

一旦您明白了这一点,就很容易理解如何设置测试.使用PHPUnit测试您的php文件和类,并使用您选择的Javascript框架来测试Javascript代码.

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.

关于将Javascript代码与php代码混合在一起:

Regarding Javascript code being mixed with php code:

  • 严格来说,即使Java代码是由php编写的,它也最终会发送到浏览器.因此在Javascript环境中运行它应该是可测试的.

  • 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.

但是实际上,这可能表明应用程序结构不良,其中Javascript代码没有与后端代码分开,并且可能没有使用对象和关注点分开.这将使代码很难测试.

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的测试最困难的部分是能够编写可测试的代码,而不是编写实际的测试.如果您的代码不使用对象,某种类型的依赖注入,用于编写HTML的模板和Javascript代码未与后端代码完全分开,那么我想您将很难进行测试.我的建议是,您使用最佳实践重写应用程序的某些部分并尝试对其进行测试.使用框架通常会有所帮助,因为框架可以将关注点分离并通常将单元测试集成在一起.

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.

这篇关于同时具有php和javascript的应用程序的测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆