我有一个使用 try...catch 的 Symfony 控制器。我使用 phpunit 来测试我的应用程序。我已经搜索过,但还没有找到如何在 catch 异常中测试代码的方法。如何强制 php 单元假装出现问题并进入 catch 块并对其进行测试?
IE:
try {
$foo = 1;
} catch (\Exception $ex) {
$mail = new Mail();
$mail->sendMail();
return new Response();
}
Run Code Online (Sandbox Code Playgroud)
我如何告诉 phpunit 抛出 \Exception 以便它测试上面的 catch 块内的代码?
你们中有人知道可以关闭标题中提到的行为的解决方法吗?每次我在测试步骤中做错事时,异常都会如下所示:
Test method
FunctionalTests.Features.Parties.PartyAddingFeature.FeatureTitle threw exception:
TechTalk.SpecFlow.BindingException: Error calling binding method 'FunctionalTests:FunctionalTests.Steps.PartiesSteps.GivenLegacyPartyUpsertedIntegrationEventCameInWithData(String, Table)': Object of type 'System.String' cannot be converted to type 'System.Int32'.
at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration) in D:\a\1\s\TechTalk.SpecFlow\Bindings\BindingInvoker.cs:line 69
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments) in D:\a\1\s\TechTalk.SpecFlow\Infrastructure\TestExecutionEngine.cs:line 395
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance) in D:\a\1\s\TechTalk.SpecFlow\Infrastructure\TestExecutionEngine.cs:line 316
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnAfterLastStep() in D:\a\1\s\TechTalk.SpecFlow\Infrastructure\TestExecutionEngine.cs:line 132
at FunctionalTests.Features.Parties.PartyAddingFeature.ScenarioCleanup()
at FunctionalTests.Features.Parties.PartyAddingFeature.FeatureTitle() in C:\Project\test\Services\FunctionalTests\Features\Parties\PartyAdding.feature:line 25
Run Code Online (Sandbox Code Playgroud) 我们为 Excel 开发基于 Office-js 的插件,我们想要为 Windows 上的桌面版 Excel 编写自动化功能测试(最好使用 Selenium)。
我们遇到的第一个问题是 Excel/Windows 的不同组合在下面使用 IE/Edge 的 webview。让 IE 死掉,假设我们只需要使用 Edge webview 进行测试(EdgeHTML,因为 Edge-chromium webview 尚未发布)。
所以主要问题是:我们无法(或不知道如何)将插件的 webview 与 Selenium 连接,因为我们不负责启动 webview 的进程 - 它是由 Excel 本身启动的。有没有推荐的方法来运行带有插件的桌面版 Excel 的功能测试?我们可以通过 COM 来控制 Excel 本身,所以这不是问题。但是如何控制addin下面的webview呢?
我们尝试过WinAppDriver,但它提供了生成窗口的特定于 Windows 的 XML,而不是我们希望看到的插件的 HTML,这使得导航和断言变得脆弱而复杂。
我认为我们到处搜索但找不到任何漂亮的解决方案,所以肮脏的黑客也是允许的,只要它们能够可靠地工作并且不会太慢。
selenium functional-testing office-addins excel-addins office-js
我们的工作场所要求自动化Web服务测试.我们一直在使用QTP脚本.
我们作为一个团队,倾向于Jbehave作为一种选择.JBehave是Web服务功能测试自动化的不错选择吗?
我们确实使用Soap UI手动测试.但我们计划自动化功能和回归测试,以缩短发布周期时间.
建议欢迎.
我正在寻求帮助,因为我不知道该怎么处理这个错误......所以首先让我展示一下我在运行真正简单的测试时得到的堆栈跟踪,奇怪的是我的测试似乎没有被执行......?我正在使用Grails 2.0
Configuring Spring Security Core ...
... finished configuring Spring Security Core
| Server running. Browse to http://localhost:8080/
| Running 3 functional tests... 1 of 0
| Failure: initializationError(SecondaryTest)
| java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:166)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:102)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:344)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:74)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:55)
| Running 3 functional tests... 2 of 0
| Failure: initializationError(SecondaryTest)
| java.lang.Exception: The @Rule '_gebReportingSpecTestName' must be public.
at org.junit.internal.runners.rules.RuleFieldValidator.addError(RuleFieldValidator.java:90)
at org.junit.internal.runners.rules.RuleFieldValidator.validatePublic(RuleFieldValidator.java:67)
at org.junit.internal.runners.rules.RuleFieldValidator.validateField(RuleFieldValidator.java:55)
at org.junit.internal.runners.rules.RuleFieldValidator.validate(RuleFieldValidator.java:50)
at org.junit.runners.BlockJUnit4ClassRunner.validateFields(BlockJUnit4ClassRunner.java:170)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:344)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:74) …Run Code Online (Sandbox Code Playgroud) 我正在测试RESTful API,需要测试应用程序是否正确解析传入的数据.传入数据的类型可以是XML或JSON格式.应用程序查看标头中的Content-Type值以确定如何处理数据.
应用程序运行良好,但我无法在Symfony2中为它编写适当的功能测试,因为我似乎无法在调用URL时设置Content-Type.
我正在做这样的事情:
$crawler = $client->request('PUT','/api/record/, array("data" => $xmlData), array(), array("Content-Type" => "text/xml"));
Run Code Online (Sandbox Code Playgroud)
虽然它没有抛出任何错误,但我的应用程序无法接收它.我用Firefox的"Poster"插件以及cUrl成功测试了我的脚本.
任何帮助/想法将不胜感激.
编写功能测试和DOMCrawler时遇到问题.我的问题是通过链接抓取邮件内容.从文档中我看到可以使用html内容作为参数创建爬虫.所以这是我的代码块:
$mailCrawler = new Crawler($message->getBody());
$linkCrawler = $mailCrawler->selectLink('Link name');
$client->click($linkCrawler->link());
Run Code Online (Sandbox Code Playgroud)
在第三行,我有一个例外,因为$ linkCrawler有空的$ uri字段.异常消息:
InvalidArgumentException: Current URI must be an absolute URL ("").
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我为什么爬虫不能获得该链接?
我只能告诉$ message var getBody方法返回正确的内容.
问候
我发现了一个很棒的网站,讨论使用IronPython对Python GUI应用程序进行功能测试:http://www.voidspace.org.uk/python/articles/testing/但是我想使用Tkinter并且很难在图书馆.
Michael为IronPython展示了这个例子:
class FunctionalTest(TestCase):
def setUp(self):
self.mainForm = None
self._thread = Thread(ThreadStart(self.startMultiDoc))
self._thread.SetApartmentState(ApartmentState.STA)
self._thread.Start()
while self.mainForm is None:
Thread.CurrentThread.Join(100)
def invokeOnGUIThread(self, function):
return self.mainForm.Invoke(CallTarget0(function))
Run Code Online (Sandbox Code Playgroud)
...而且我很难将其转换为如何连接到基于Tkinter的应用程序,该应用程序将具有基本设置:
from tkinter import *
from tkinter import ttk
root = Tk()
ttk.Button(root, text="Hello World").grid()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
...我想你也想在第二个线程中的主根对象上运行一个方法,但是我没有看到与mainForm.Invoke()相同的方法.也许我在想错了.也许以这种方式进行功能测试GUI应用并不常见?
例子真棒!
我看到有很多方法可以将夹具数据加载到数据库中.但在功能测试之后,确认写入数据库的内容的最佳/标准方法是正确的?
phpunit包有一个完整的部分,你可以在其中加载数据集,然后使用assertTablesEqual()之类的东西来比较表的内容和预期的内容.但这似乎在Symfony2下不可用,我找不到任何其他标准方法.
别人怎么解决这个问题?
我很难理解我的功能测试或项目设置有什么问题:phpunit执行只打印出以下信息(我不是在测试套件中打印出来的 - 即它不是来自客户端 - > getResponse()打印或任何东西).此外,在将此文本打印到命令行后,整个测试执行立即停止,没有任何结果信息:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="1;url=/" />
<title>Redirecting to /</title>
</head>
<body>
Redirecting to <a href="/">/</a>.
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
从命令行运行phpunit后:
phpunit -c app --group temp1 src/AppBundle/Tests/Controller/SecurityControllerTest.php
Run Code Online (Sandbox Code Playgroud)
我的测试代码很简单:
class SecurityControllerTest extends WebTestCase
{
/**
* test login with succesfull case
*
* @group login
* @group temp1
*/
public function testLogin_success()
{
$client = static::createClient();
$crawler = $client->request('GET', '/');
// temp - just to test that the initial crawler location is correct …Run Code Online (Sandbox Code Playgroud) symfony ×5
php ×3
phpunit ×3
automation ×1
c# ×1
excel-addins ×1
geb ×1
grails ×1
grails-2.0 ×1
jbehave ×1
office-js ×1
python ×1
selenium ×1
specflow ×1
spock ×1
testing ×1
tkinter ×1
unit-testing ×1
web-services ×1