这是一个示例程序:
public class FunctionalTest {
public int f(int r) {
int result = r * 5;
return result;
}
public static void main(String[] args) {
FunctionalTest funct = new FunctionalTest();
System.out.println(funct.f(5));
}
}我是个初学者。如何为这段代码编写功能测试?如何编写功能测试?我需要TestNG吗?写考试方法足够了吗?有人能给我解释一下并为这个程序编写一个功能测试样本吗?
发布于 2014-04-15 22:09:37
首先,您需要对要验证的合同有一个明确的定义。从代码中,我假设它类似于“方法应该返回等于参数乘以5的数字”。
对于您的情况,TestNG、JUnit或其他测试框架不是必需的。该测试看起来可能如下:
public void testF() {
int arg = 5;
int result = new FunctionalTest().f(arg);
assert result == arg * 5;
}另外,请记住,要使用assert,您需要使用-ea标志。
发布于 2014-04-15 22:25:21
好吧,如果您专门要求进行功能测试,那么您就没有什么办法处理代码片段了。您可以使用这样的JUnit从f方法进行单元测试:
@Test
public void testF(){
FunctionalTest t1 = new FunctionalTest();
assertEquals((t1.f(1) % 5), 0); //checks that is getting multiplied by 5.
}但是,您需要功能测试,因此通过运行编译后的应用程序并评估结果,您将通过多个单元(AKA集成)测试应用程序的功能:f方法和主要方法。
致以问候!
发布于 2015-07-06 10:42:38
小心你使用的术语:
这意味着:
您可以使用任何您想要测试的特性(从单元测试到j表现)。
在您的情况下(使用JUnit 4和AssertJ):
import org.assertj.core.api.Assertions;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
/*
As an user
I want have 25 whatever I sent
*/
public class NumberGenerationTest {
private static final String PATH = "directory of your class file";
private InputStream stdout;
/* Nominal case */
@Test
public void shall_return_number_25_when_called_with_5() throws Exception {
when_I_call_FunctionalTest_with("5");
then_it_returns("25");
}
/* Nominal case or potential error case */
@Test
public void shall_return_number_25_when_called_with_10() throws Exception {
when_I_call_FunctionalTest_with("10");
then_it_returns("25");
}
/* Nominal case or potential error case */
@Test
public void shall_return_number_25_when_called_with_ABC() throws Exception {
when_I_call_FunctionalTest_with("ABC");
then_it_returns("25");
}
private void when_I_call_FunctionalTest_with(String parameter) throws Exception {
ProcessBuilder builder = new ProcessBuilder("java" ,"-classpath", PATH,"FunctionalTest" , parameter);
builder.redirectErrorStream(true);
Process process = builder.start();
stdout = process.getInputStream ();
}
private void then_it_returns(String expectedResult) throws Exception {
BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
String line = reader.readLine ();
Assertions.assertThat(line).isNotNull();
Assertions.assertThat(line).isEqualTo(expectedResult);
}
}好像你的主()有个错误.或者不是。
https://stackoverflow.com/questions/23095382
复制相似问题