如何用java编写简单的txt测试用例
您好,我正在编写我的第一个 Java 应用程序,我有一些测试用例(*.tc 文件),我想通过此脚本直接访问该应用程序:
for f in `ls *.tc`; do
echo "Current Testcase: $f"
x=${f%.*}
java Main < $x.tc > $x.out
if diff "$x.out" "$x.should"; then
echo "passed testcase $f"
let PASSED=PASSED+1
else
echo "failed testcase $f"
let FAILED=FAILED+1
fi
done
问题是我无法弄清楚为什么一旦 < code>tc 文件 包含不止一行,应用程序变得疯狂。例如: quit.tc
包含
quit
并工作就像我手动输入“quit”时一样,因此测试用例通过。 但是,当我编写另一个 tc: quit2.tc
时,其中包含
lala
test
quit
应用程序在第一个命令后退出(因为 readString 函数似乎随后返回 null)。 这是负责读取的函数:
public String readString(){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String answer = null;
try {
answer = br.readLine();
return answer;
}
catch(IOException ioe) {
System.out.println("IO Error");
}
return answer;
}
当我重定向到应用程序时,我不知道为什么或何时此函数返回 null,这似乎是问题所在。你能帮我让 tc 脚本工作吗?谢谢
Hi I'm writing my first Java app and I've got a few Testcases (*.tc files) I want to direct to the app via this script:
for f in `ls *.tc`; do
echo "Current Testcase: $f"
x=${f%.*}
java Main < $x.tc > $x.out
if diff "$x.out" "$x.should"; then
echo "passed testcase $f"
let PASSED=PASSED+1
else
echo "failed testcase $f"
let FAILED=FAILED+1
fi
done
The Problem is I can't quite figure out why as soon as the tc file
contains more than one line the app goes nuts. For example: quit.tc
contains
quit
and works just like when I manually enter "quit", therfore the testcase passes.
However when I write another tc: quit2.tc
which contains
lala
test
quit
The app quits after the first command (because the readString function seems to return null afterwards).
Here is the function responsible for reading:
public String readString(){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String answer = null;
try {
answer = br.readLine();
return answer;
}
catch(IOException ioe) {
System.out.println("IO Error");
}
return answer;
}
I dont know why or when this function returns null when I redirect to the app, which seems to be the problem. Can you help out so I can get the tc script working? thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您是 java 新手,并且仍在塑造自己的风格和做事方式,我会向您推荐 2 个提示:
1) 使用 扫描仪来读取您的输入。使用 nextLine()< Scanner 对象的 /a> 方法可能就是您正在寻找的。
2) 设计您的代码,以便可以通过 JUnit 进行测试。
不过,在您的
readString()
方法中,从 try 块中删除return answer;
。更新:尝试在您的函数中实现以下内容:
a),而扫描仪实例 hasNextLine() 为 true ->
b) 调用扫描器实例的 nextLine() 方法 ->
c) 解析该行,看看它是否等于 'quit' ->
d) 实现相应的逻辑
if
情况。If you're new to java, and you still shape your style and way of doing things, I will recommend you 2 tips:
1) use Scanner to read your input. Using nextLine() method of the Scanner object is probably what you're looking for.
2) design your code, so that it's testable via JUnit.
Still, in your
readString()
method, remove thereturn answer;
from the try block.UPDATE: try to implement the following in your function:
a) while the scanner instance hasNextLine() is true ->
b) call scanner instance nextLine() method ->
c) parse the line, and see if it equals 'quit' ->
d) implement the corresponding logical
if
cases.您的问题是 BufferedReader:它将立即读取整个文件(出于性能原因)。
由于每次调用
readString()
时都会创建一个新的BufferedReader
,因此第一个读取器将吞下文件的大部分(或全部),而第二个读取器将结束- 文件。解决方案是将读取器放入一个字段中,创建一次一次,然后始终使用相同的读取器来调用
readLine()
另外,您永远不会关闭读取器。
Java 会忘记未使用的对象,但您必须正确关闭操作系统资源,例如 FileInputStream
Your problem is
BufferedReader
: It will read the whole file at once (for performance reasons).Since you create a new
BufferedReader
each timereadString()
is called, the first reader will swallow most of the file (or all of it) and the second reader will hit end-of-file.The solution is to put the reader into a field, create it once and then always use the same reader to call
readLine()
Plus you never close the reader.
Java will forget unused objects but you must properly close OS resources like
FileInputStream
s