我正在尝试对一个类进行单元测试,其中它的方法之一返回协作者类的实例:根据其参数的值,它要么返回新创建的实例,要么返回保存的先前创建的实例。
我在 Expectations 中模拟构造函数调用,并将结果设置为协作者的模拟实例值。但是,当我使用导致该方法创建新实例的参数值测试该方法时,模拟的构造函数以及该方法不会返回预期值。
我已将其简化为以下内容:
package com.mfluent;
import junit.framework.TestCase;
import mockit.Expectations;
import mockit.Mocked;
import mockit.Tested;
import org.junit.Assert;
import org.junit.Test;
public class ConstructorTest extends TestCase {
static class Collaborator {
}
static class ClassUnderTest {
Collaborator getCollaborator() {
return new Collaborator();
}
}
@Tested
ClassUnderTest classUnderTest;
@Mocked
Collaborator collaborator;
@Test
public void test() {
new Expectations() {
{
new Collaborator();
result = ConstructorTest.this.collaborator;
}
};
Collaborator collaborator = this.classUnderTest.getCollaborator();
Assert.assertTrue("incorrect collaborator returned", collaborator == this.collaborator);
}
}
Run Code Online (Sandbox Code Playgroud)
任何有关此测试失败的原因以及如何使其发挥作用的想法都将不胜感激。
提前致谢,
Jim Renkel …
如何使用sonarlint来分析测试用例?我在声纳立方体中找到了类似的东西sonar.test.inclusions作为属性,但在中找不到类似的东西sonarlint > intellij.
我需要分析的文件夹:src/test/java
解决办法:
重命名 test 文件夹对我有用。我将 test 更改为 test1。我是否可以遵循并可以分析测试用例的正确方法/步骤?
HTML 代码:
<p class="target">
<span>I am Span 1 of target_value 1*</span>
<span>I am Span 2 of target_value 2*</span>
target_value /* I want to get this value in cypress test case */
</p>
Run Code Online (Sandbox Code Playgroud)
注意*:文本“I am Span 1 of target_value 1”和“I am Span 2 of target_value 2”都是动态的,有时会发生变化。但这些跨度可能包含文本“target_value”。在 cypress 测试用例中,如何选择文本“target_value”
直接(不在其子级中)并检查它是否正在渲染。我只想获取不在其任何子元素内的主文本值。当我构建我的测试项目时,我发现了非常奇怪的东西.
我从AndroidTestCase继承了自己的测试,并在里面实现了几个测试用例.有些案例最初没有运行,但没有例外.在我刚刚更改了这些案例的名称之后,它们被测试运行器调用并且全部通过了.
我不知道是否有任何命名约定.
我们正在使用NUnit来运行我们的集成测试.其中一个测试应始终相同,但采用不同的输入参数.不幸的是,我们不能使用[TestCase]属性,因为我们的测试用例存储在外部存储中.我们有动态测试用例,我们的QA工程师可以添加,删除或禁用(不删除).QA人员无法将[TestCase]属性添加到我们的C#代码中.他们所能做的就是将它们添加到存储中.
我的目标是将存储中的测试用例读入内存,使用所有已启用的测试用例运行测试,报告测试用例是否失败.我不能使用"foreach"语句,因为如果测试用例#1失败,那么其余的测试用例将根本不运行.我们已经有了生成NUnit报告的构建服务器(CruiseControl.net),因此我想继续使用NUnit.
你能指出一种如何实现我的目标的方法吗?谢谢.
我正在使用lcov为C代码库生成代码覆盖率报告.我想将测试描述集成到最终输出中(使用lcov的gendesc实用程序.)
但是,我不清楚如何做到这一点,并且关于gendesc的文档看起来相当稀疏(就好的旧谷歌能够告诉我的那样.)
LTP的gendesc信息描述了如何创建输入测试用例描述文件(如genhtml所预期的那样).而GENHTML信息提供--show-descriptions,以及--description-file用于输入这样的测试案例描述文件.
但是,我不知道如何引用测试用例以便将它们包含在最终报告中.genhtml将它们视为未使用的测试用例,从而使它们不受生成的html输出的影响.我可以使用--keep-descriptions,但这并没有告诉我运行了什么测试用例(显然是因为我不知道如何从代码中引用测试描述.)
那么,我们如何告诉lcov/genhtml哪些测试在最终输出中运行?有任何想法吗?
我正在编写一个测试用例,它将有一个SetUpTestCase()分配共享资源的方法,尽管我收到的是未定义的引用链接器错误.
class ParsingEventsTest: public ::testing::Test {
protected:
static xml eventXml;
static void SetUpTestCase() {
ManagedObjectManagerSingleton::GET_SINGLETON().initializeTestEnvironment(PATH_TO_FILE);
eventXml= *ManagerSingleton::GET_SINGLETON().parse(PATH_TO_INPUT_FILES);
}
virtual void SetUp() {}
virtual void TearDown() {}
};
Run Code Online (Sandbox Code Playgroud)
这会导致:
../test/ParsingEventsTest.o: In function `ParsingEventsTest::SetUpTestCase()':
ParsingEventsTest.cpp:(.text._ZN17ParsingEventsTest13SetUpTestCaseEv[ParsingEventsTest::SetUpTestCase()]+0xa1): undefined reference to `ParsingEventsTest::eventXml'
ParsingEventsTest.cpp:(.text._ZN17ParsingEventsTest13SetUpTestCaseEv[ParsingEventsTest::SetUpTestCase()]+0xb0): undefined reference to `ParsingEventsTest::eventXml'
ParsingEventsTest.cpp:(.text._ZN17ParsingEventsTest13SetUpTestCaseEv[ParsingEventsTest::SetUpTestCase()]+0xbd): undefined reference to `ParsingEventsTest::eventXml'
ParsingEventsTest.cpp:(.text._ZN17ParsingEventsTest13SetUpTestCaseEv[ParsingEventsTest::SetUpTestCase()]+0xc2): undefined reference to `ParsingEventsTest::eventXml'
ParsingEventsTest.cpp:(.text._ZN17ParsingEventsTest13SetUpTestCaseEv[ParsingEventsTest::SetUpTestCase()]+0xce): undefined reference to `ParsingEventsTest::eventXml'
../test/ParsingEventsTest.o:ParsingEventsTest.cpp:(.text._ZN17ParsingEventsTest13SetUpTestCaseEv[ParsingEventsTest::SetUpTestCase()]+0xdd): more undefined references to `ParsingEventsTest::eventXml' follow
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
编辑:
这也适用于非常简单的例如int分配
class ParsingEventsTest: public ::testing::Test { …Run Code Online (Sandbox Code Playgroud) Django 1.5和Python 2.6.
该模型有一个clean()验证job.company_id必须相等的方法job.location.company_id
我正在尝试为此编写测试,但测试不是通过/未通过测试,而是以模型clean()方法的验证错误消息结束.
这是代码(省略了无关的位):
在models.py中:
class Job(models.Model):
title = models.CharField(max_length=200, verbose_name="Job title")
company = models.ForeignKey(Company)
location = models.ForeignKey(Location, blank=True, null=True)
def clean(self):
from django.core.exceptions import ValidationError
'''
Location.company_id must equal Job.company_id
'''
if (self.company_id != Location.objects.get(pk=self.location_id).company_id):
raise ValidationError('Location is not valid for company')
Run Code Online (Sandbox Code Playgroud)
在tests.py中:
class job_cannot_have_invalid_location_and_can_have_valid_location(TestCase):
def test_jobs_and_locations(self):
job2 = Job.objects.create(company_id=company2.id)
location1 = Location.objects.create(company_id=company1.id)
job2.location_id = location1.id
self.assertRaises(ValidationError, job2.clean())
Run Code Online (Sandbox Code Playgroud)
当我运行python manage.py测试时:
.E.
======================================================================
ERROR: test_jobs_and_locations (companies.tests.job_cannot_have_invalid_location_and_can_have_valid_location)
----------------------------------------------------------------------
Traceback (most recent call …Run Code Online (Sandbox Code Playgroud) 我正在使用assertEquals()中unittest.TestCase。我现在想做的是调用一个函数,并在断言失败时在其中执行某项操作,我想知道是否有这样做的方法?
如何'Authorization': 'Token'在Django/DRF中添加TEST请求?
如果我使用简单的requests.get(url, headers={'Authorization': 'Token'}所有工作完美但如何在TestCase中做这样的请求?
testcase ×10
python ×2
unit-testing ×2
android ×1
assert ×1
c++ ×1
constructor ×1
cypress ×1
django ×1
django-tests ×1
googletest ×1
html ×1
java ×1
jmockit ×1
junit ×1
lcov ×1
nunit ×1
python-2.6 ×1