我刚与我的首席开发人员进行过对话,他们不同意单元测试是必要或重要的.在他看来,具有足够高的代码覆盖率的功能测试应该足够,因为任何内部重构(接口改变等)都不会导致需要重写或重新检查测试.
我尝试了解释,但没有走得太远,并认为你们可以做得更好.;-)所以......
对功能测试不提供的单元测试代码有什么好的理由?如果您拥有所有功能测试,会有什么危险?
编辑#1感谢所有的好答案.我想通过功能测试补充一点,我并不仅仅意味着对整个产品进行测试,而是对产品中的模块进行测试,而不是在必要时进行模拟的单元测试的低级别等等.另请注意我们的功能测试是自动的,并且持续运行,但它们只需要比单元测试更长的时间(这是单元测试的一大优势).
我喜欢砖与房子的例子.我想我的主要开发人员说的是测试房子的墙壁就足够了,你不需要测试个别砖...... :-)
我正在使用粘贴在我的web.py应用程序中对我的'控制器'进行一些功能测试.在一个案例中,我正在尝试在对API端点进行格式错误的帖子时测试400响应.这是我的测试的样子:
def test_api_users_index_post_malformed(self):
r = self.testApp.post('/api/users', params={})
assert r.header('Content-Type') == 'application/json'
assert r.status == 400
Run Code Online (Sandbox Code Playgroud)
但我得到以下异常:
AppError: Bad response: 400 Bad Request (not 200 OK or 3xx redirect for /api/users)
Run Code Online (Sandbox Code Playgroud)
我看膏具有HttpException中间件,但我无法找到如何使用它,或者如果连正确的方式去任何例子.有什么建议?或者我只是错了?
我正在尝试用Mockito和Dagger测试一项活动 .我已经能够在我的应用程序中为Activity注入依赖项,但在测试Activity时,我无法向Activity注入mock.我应该注入Activity来测试还是让getActivity()创建它?
public class MainActivityTest extends
ActivityInstrumentationTestCase2<MainActivity> {
@Inject Engine engineMock;
private MainActivity mActivity;
private Button mLogoutBtn;
public MainActivityTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
// Inject engineMock to test
ObjectGraph.create(new TestModule()).inject(this);
}
@Override
protected void tearDown() {
if (mActivity != null)
mActivity.finish();
}
@Module(
includes = MainModule.class,
entryPoints = MainActivityTest.class,
overrides = true
)
static class TestModule {
@Provides
@Singleton
Engine provideEngine() {
return mock(Engine.class);
}
}
@UiThreadTest
public void testLogoutButton() { …Run Code Online (Sandbox Code Playgroud) android dependency-injection functional-testing mockito dagger
我正在使用Play Framework 2.3和IntelliJ IDEA 14.我在我的应用程序中使用了Mailer插件.test在将此行添加到build.sbt之后,我在SBT控制台中运行命令时编写了一些完美的功能测试:
javaOptions in Test += "-Dconfig.file=conf/application.test.conf"
Run Code Online (Sandbox Code Playgroud)
这一个到文件conf/application.test.conf:
smtp.mock=yes
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我直接从IntelliJ运行测试时,我收到此错误:
java.lang.RuntimeException: smtp.host needs to be set in application.conf in order to use this plugin (or set smtp.mock to true)
Run Code Online (Sandbox Code Playgroud)
我尝试使用VM参数启动这些测试-Dconfig.file=conf/application.test.conf,但没有成功.
以下是我尝试执行的测试的两个示例:
@Test
public void testWithServer() {
running(testServer(3333), () -> {
assertThat(WS.url("http://localhost:3333").get().get(1000).getStatus()).isEqualTo(OK);
});
}
@Test
public void testWithBrowser() {
running(testServer(3333), HTMLUNIT, browser -> {
browser.goTo("http://localhost:3333");
assertThat(browser.$("title").getText()).isEqualTo("Welcome");
});
}
Run Code Online (Sandbox Code Playgroud)
谁可以帮我这个事?
谢谢!
我正在计划一个Apigility驱动的RESTful Zend Framework 2应用程序.对于单元测试,也可能用于数据库测试,将使用PHPUnit.现在我要为应用程序定义功能测试.
"功能测试"对我来说意味着测试真正的功能.它还获得了集成测试方面,因为应用程序然后进行"intermodularily"测试,因此它是跨单元/模块的测试. (我对功能测试的理解是否正确?)
对于此测试,将发送实际请求并将响应与期望进行比较.随着写入请求可能会有点复杂,但为了保持简单,我们GET首先考虑这种情况.
(对?)
为此目的,使用行为测试似乎是有目共睹的.(实际上我根本没有看到任何其他适当的方法.) (对吧?)
如果我的逻辑步骤之一是假的,请纠正我.
可以在RESTful PHP(ZF2)应用程序的上下文中使用哪些行为测试工具?PHPUnit故事扩展?那是什么?phpspec?其他框架?或者也许直接测试PHPUnit(定义一个单独的测试套件并在其测试类中使用API调用执行行为测试)?
或者这一切都是错误的,功能测试需要一种完全不同的方法吗?
我真的很喜欢selenium 2按照惯例推动你使用PageObjects作为POJO,然后简单地使用PageFactory来实例化这个类中的字段.
我发现限制的是我们在许多不同的页面上重用了很多元素.最大的问题是这些重用的组件在出现在不同页面时没有相同的id/name; 但是我们为每个测试运行的测试是相同的.
举个例子,我们在很多地方收集日期.因此,此示例页面对象可能是(月,日字段已删除):
public class DatePageObject {
private WebDriver driver;
DatePageObject(WebDriver driver) {
this.driver = driver;
}
@FindBy( id = "someIdForThisInstance")
private WebElement year;
public void testYearNumeric() {
this.year.sendKeys('aa');
this.year.submit();
//Logic to determine Error message shows up
}
}
Run Code Online (Sandbox Code Playgroud)
然后我可以使用下面的代码简单地测试一下:
public class Test {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
DatePageObject dpo = PageFactory.initElements(driver, DriverPageObject.class);
driver.get("Some URL");
dpo.testYearNumeric();
}
}
Run Code Online (Sandbox Code Playgroud)
我真正想做的是有一个设置,通过Spring我可以将id/name/xpath等注入到应用程序中.
有没有办法可以做到这一点,而不会失去使用PageFactory的能力?
编辑1 - 添加理想的基础级别,处理自定义定位器和工厂.
public class PageElement {
private WebElement element;
private …Run Code Online (Sandbox Code Playgroud) 我得到"InvalidArgumentException:当前节点列表为空." 通过PHPUnit运行功能测试.这是我写的测试:
public function testAdd()
{
$client = static::createClientWithAuthentication('main');
$crawler = $client->request('GET', 'en/manage');
$send_button = $crawler->selectButton('submit');
$form = $send_button->form(array(
'PrCompany[email]' => 'test@example.ua',
'PrCompany[first_name]' => 'Anton',
'PrCompany[last_name]' => 'Tverdiuh',
'PrCompany[timezone]' => 'Europe/Amsterdam'
));
$form['PrCompany[companies][1]']->tick();
$client->submit($form);
$this->assertTrue($crawler->filter('html:contains("User is invited")')->count() > 0);
}
Run Code Online (Sandbox Code Playgroud) 我在编写Symfony 2功能测试时遇到问题,无法设置属于数组的复选框(即多个扩展的选择窗口小部件)
在文档中,示例是
$form['registration[interests]']->select(array('symfony', 'cookies'));
Run Code Online (Sandbox Code Playgroud)
但它没有显示哪些html可以使用它并且它不适用于我的.这是我的表格的缩减版本
<form class="proxy" action="/proxy/13/update" method="post" >
<input type="checkbox" id="niwa_pictbundle_proxytype_chronologyControls_1" name="niwa_pictbundle_proxytype[chronologyControls][]" value="1" />
<input type="checkbox" id="niwa_pictbundle_proxytype_chronologyControls_2" name="niwa_pictbundle_proxytype[chronologyControls][]" value="2" />
<input type="checkbox" id="niwa_pictbundle_proxytype_chronologyControls_3" name="niwa_pictbundle_proxytype[chronologyControls][]" value="3" />
</form>
Run Code Online (Sandbox Code Playgroud)
一旦它在那里工作,我将转向手动制作的表格
<input type="checkbox" id="13" name="proxyIDs[]" value="13">
<input type="checkbox" id="14" name="proxyIDs[]" value="14">
<input type="checkbox" id="15" name="proxyIDs[]" value="15">
Run Code Online (Sandbox Code Playgroud)
我尝试过类似的东西
$form = $crawler->selectButton('Save')->form();
$form['niwa_pictbundle_proxytype[chronologyControls]']->select(array('3'));
$form['niwa_pictbundle_proxytype[chronologyControls][]']->select(array('3'));
Run Code Online (Sandbox Code Playgroud)
但第一个失败的说法select是在一个非对象上运行,第二个说 Unreachable field "".
我有一个用Symfony 2编写的HTTP API,我正在为它编写一些功能测试.
我注意到当我尝试发送Authorization标题时,在我记录收到的标题时,控制器中没有收到它.
在测试控制器中:
$client = self::createClient();
$client->insulate();
$headers = array(
'Authorization' => "Bearer {$accessToken}",
'CONTENT_TYPE' => 'application/json',
);
$client->request('DELETE', "/auth", array(), array(), $headers );
Run Code Online (Sandbox Code Playgroud)
在测试的控制器中:
print_r( $request->headers );
Run Code Online (Sandbox Code Playgroud)
输出:
Symfony\Component\HttpFoundation\HeaderBag Object
(
[headers:protected] => Array
(
[host] => Array
(
[0] => localhost
)
[user-agent] => Array
(
[0] => Symfony2 BrowserKit
)
[accept] => Array
(
[0] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
)
[accept-language] => Array
(
[0] => en-us,en;q=0.5
)
[accept-charset] => Array
(
[0] => ISO-8859-1,utf-8;q=0.7,*;q=0.7 …Run Code Online (Sandbox Code Playgroud) 我正在编写一个使用我创建的REST API的Django应用程序.目的是使用Web应用程序证明api用例.在我看来,我因此使用python-requests库调用api,如下所示:
def my_view_method(request):
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
data = form.cleaned_data
data_to_post = {
'fieldA': data.get('fieldA_in_form'),
'fieldB': data.get('fieldB_in_form'),
}
post_url = "http://%s/%s/" % (request.get_host(), 'entries')
logger.info("request api url: "+ post_url)
r = requests.post(post_url, data=data_to_post)
return HttpResponseRedirect('/')
else:
form = MyForm()
return render(request, 'myview.html', { 'form': form })
Run Code Online (Sandbox Code Playgroud)
我已经使用单元测试验证了使用有效数据POST到/ entries /会导致正确的数据库更新.
url = '/entries/'
#verify initial db state
data = { 'fieldA': value1, 'fieldB': value2 }
response = self.client.post(url, data, format='json')
# verify db was updated …Run Code Online (Sandbox Code Playgroud)