gpt4 book ai didi

How to pass resource bytes as parameters(如何将资源字节作为参数传递)

转载 作者:bug小助手 更新时间:2023-10-25 10:40:10 28 4
gpt4 key购买 nike



I have a Test method foo. I have test input large.zst in test/resources.
Now I would like to pass the bytes of large.zst as byte[] into foo.

我有一个测试方法Foo。我在测试/资源中有测试输入Large.zst。现在,我想将large.zst的字节作为byte[]传递给foo。


Is there any way of passing the file content as a byte array via Annotations?

有没有办法通过注释将文件内容作为字节数组传递?


更多回答
优秀答案推荐

I do not know how to do this for TestNG, but with JUnit, I would abuse a value source and implement an ArgumentProvider.

我不知道如何为TestNG做这件事,但是使用JUnit,我会滥用一个值源并实现一个ArgumentProvider。


public class LargeZSTAsBytesArgumentsProvider implements ArgumentsProvider 
{
@Override
public final Stream<? extends Arguments> provideArguments( ExtensionContext context )
{
final var path = Paths.get( ".", "test", "resources", "large.zst" );
final var bytes = Files.readAllBytes( path );

final var retValue = Stream.of( Arguments.of( bytes ) );
return retValue;
}
}

Basically, you get the file as an instance of java.nio.Path, then you call Files::readAllBytes with that Path instance and wraps the resulting byte array.

基本上,您将该文件作为一个java.nio.Path实例获取,然后使用该Path实例调用Files::ReadAllBytes并包装结果字节数组。


foo() will then look like this:

Foo()将如下所示:


@ParameterizedTest
@ArgumentsSource( LargeZSTAsBytesArgumentsProvider.class )
void foo( final byte [] input )
{
// do something with the byte array …
}

Obviously, the error handling is missing!

显然,错误处理缺失了!



Adapting the answer from @tquadrat to testng would look like this IMO:

将@tsquare的答案改写为TestNG,如下所示:


    @DataProvider(name = "largeCompressed")
private Object[] createLargeCompressed() {
// Had no luck using `new Path`.
URL url = this.getClass().getResource("/large.zst");
Path path;
try {
path = Path.of(url.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
try {
return new Object[]{Files.readAllBytes(path)};
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Test(dataProvider = "largeCompressed")
public void foo(byte[] largeCompressed) {
// do something with the byte array …
}


To pass the bytes of a file (e.g., "large.zst" located in test/resources) as a parameter to a test method using annotations in JUnit 5, you can use the @ParameterizedTest annotation along with @MethodSource. Here's an example of how to do this:

要使用JUnit5中的注释将文件的字节(例如,位于测试/资源中的“large.zst”)作为参数传递给测试方法,您可以使用@参数化为测试注释和@方法源。下面是一个如何做到这一点的例子:


First, create a method that provides the test data. In this case, it will read the file and return its contents as a byte array.

首先,创建一个提供测试数据的方法。在本例中,它将读取文件并以字节数组的形式返回其内容。


Annotate this method with @MethodSource to specify it as the source of test data.

使用@MethodSource注释此方法,以将其指定为测试数据源。


In your test method, accept the byte array as a parameter.

在测试方法中,接受字节数组作为参数。


Here's an example:

下面是一个例子:


import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class MyTest {

@ParameterizedTest
@MethodSource("provideFileContents")
void testFoo(byte[] fileContent) {
// Now you can use the fileContent byte array in your test method
// ... your test logic here ...
}

// This method reads the file and provides its content as a byte array
static byte[][] provideFileContents() throws IOException {
Path filePath = Path.of("src/test/resources/large.zst");
byte[] fileContent = Files.readAllBytes(filePath);
return new byte[][] { fileContent };
}
}

The provideFileContents method reads the content of the "large.zst" file and returns it as a two-dimensional byte array.

ProvideFileContents方法读取“large.zst”文件的内容并将其作为二维字节数组返回。


The @ParameterizedTest annotation is used on the test method, and @MethodSource("provideFileContents") specifies that the test data will come from the provideFileContents method.

在测试方法上使用@参数化为测试注解,而@MethodSource(“ProvideFileContents”)指定测试数据将来自ProvideFileContents方法。


Inside the testFoo method, you can use the fileContent byte array for testing your logic.

在testFoo方法内部,您可以使用fileContent字节数组来测试您的逻辑。


This approach allows you to pass resource bytes as parameters to your test method using annotations in JUnit 5.

这种方法允许您使用JUnit5中的注释将资源字节作为参数传递给测试方法。


Also I am not sure if this is what you are looking for.

我也不确定这是不是你要找的东西。


更多回答

Isn't there a more integrated way included in testng?

TestNG中没有包含更集成的方法吗?

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com