我写这篇文章是为了给我的项目写单元测试用例,这对我来说是个新手。我有一个检查给定文件是否为PDF的函数(下面的函数):
def file_verify(orig_pdf):
try:
read_pdf = PyPDF2.PdfFileRead(open(orig_pdf,'rb'))
except PyPDF2.utils.PdfReadError:
return orig_pdf, "error: Invalid PDF is not supported!"
else:
ret
我编写了这个测试,但是为了不延迟测试,我模拟time.sleep,测试将遇到失败。
from unittest.mock import patch
from django.core.management import call_command
from django.db.utils import OperationalError
from django.test import TestCase
class CommandsTest(TestCase):
@patch('time.sleep', return_value=None)
def test_wait
我用Dart (1.9.3)编写了一些简单的项目,使用unittest库进行单元测试。我在检查构造函数是否抛出错误时有问题。下面是我为这个问题编写的示例代码:
class MyAwesomeClass {
String theKey;
MyAwesomeClass();
MyAwesomeClass.fromMap(Map someMap) {
if (!someMap.containsKey('the_key')) {
throw new Exception('Invalid object forma
我正在尝试做的是用Typescript为我们已经有一段时间的现有函数库编写单元测试,它是用普通的JS编写的。我们所有的页面逻辑也都是用简单的JS编写的。其中一些函数在输入类型错误时抛出异常。例如:
Collection.pluck([{"a":1},{"a":2}], "a") // Pulls the values out of a list of objects
> [1, 2]
因此,第二个参数应该有一个字符串,即要获取其值的属性名称。在pluck函数中有一个类型检查,它验证是否传入了一个对象数组,当这个检查失败时,它抛出一个Type
我正在挑战自己,为freeCodeCamp中的问题创建自己的测试。目前停留在第二个要求“用JavaScript从一个数字中减去另一个数字”。以下是要求。 var difference = 45 - 0;
//The variable difference should be equal to 12.
//You should only subtract one number from 45. 我如何编写自己的测试来确保我只使用了两个参数? (freeCodeCamp问题可以在这里找到:https://www.freecodecamp.org/learn/javascript-al
我正在用Elrond测试框架编写集成测试。
所以,我正在运行cargo test。但是它抛出了这个错误:
note: /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
请问我怎样才能修好?
我正在尝试编写一个简单的单元测试,在这里我用接口和实现包装了HttpContext.Current.Server.MapPath。如果实现的位置正确,我就不确定了。
public class FooGenerator
{
ServerPathProvider serverPathProvider = new ServerPathProvider();
public string generateFoo()
{
BarWebService bws = new BarWebService(serverPathProvider.MapPath("~/
我有一种基于regexes的数据过滤方法。我有多个函数,每个函数都返回一个regexes列表,并将其组合成一个regex字符串。然后迭代我的数据列表,如果有任何数据与组合字符串匹配,则从数据列表中删除该项。
我想测试,当函数对一组数据运行时,过滤的项被删除,而non_filtered项没有被删除。我想测试这些数据是否与来自上述函数的每一组正则表达式相匹配。
我想要创建一个元组列表,我可以传递给参数化。每个元组将包括原始列表、已删除项列表和剩余项列表。与其为参数化创建这个元组列表作为一个全局变量,不如为此使用一个夹具吗?
另外,是否应该编写单独的函数来检查不同的行为?例如:
test that
我有一个基类,如下所示:
public abstract class BaseClass implements Runnable {
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
@Override
public void run() {
someFunction();
}
protected abstract void someFunction();
}
然后我有一个孩子班,就像这样:
public class ChildClass extends Base
我想用模拟对象编写F#单元测试。我使用的是NUnit。但不幸的是,我找不到任何例子。
以下是正在测试的代码的示例:
type ICustomer = interface
abstract Id: int with get
abstract Name: string with get
abstract CalculateBalanceWithDiscount: decimal -> decimal
end
type Customer = class
val id: int
val name: string
val balance: deci
我想确保只允许整数作为参数,所以我编写了这个测试。
describe('Assigning values', function () {
it('Should only be able to assign integers', function () {
var item = new Item();
expect(item.setValue('string').to.throw(Error, 'Not an integer'));
});
});
我正在测试的功能
var testInteger =
对于签名如下所示的方法:
public T Add<T>(T first, T second) where T : struct, IEquatable<T>, IComparable<T>
它可以与所有的积分类型一起工作,我需要为其中的每一个编写测试吗?
public void Test_Add_Int()
{
Assert.That(Add<int>(10, 20), Is.EqualTo(30));
}
public void Test_Add_UInt()
{
Assert.That(Add<uint>(10
我有一个任务,要求我修改一些代码。最初的职能是:
def selectivelyCopy(inputFile,outputFile,predicate):
linesCopied = 0
for line in inputFile:
if predicate(line):#test the line with the predicate
outputFile.write(line)
linesCopied+=1
inputFile.close()
return linesCopied
现在,我应该添加参数转换,这是一个函数,它的参数是字符串,并根
我有一个带有子类MappaFermi和属性mappaName的Mappa类(可以从Enum中选择)。
我试图用构造函数MappaFermi()来编写测试,但是我似乎不能正确地编写它。
public class Mappa {
private Name mappaName;
public Mappa (Name mappaName){
this.mappaName=mappaName;
...
}
}
public class MappaFermi extends Mappa {
public MappaFermi(Name m
我想为该函数编写单元测试用例,以测试Thread.sleep()函数是否正常工作。
有人能提供一段代码来帮助我完成这项任务吗?
public class A implements B {
public int func(String A) throws ApplicationException {
int ctr = 0;
int ans = 0;
for (int i=0;i<100020;i++) {
try {
++ctr;
if(ctr%50==0)