プログラムのログ記録に java.util.logging を使用しています。問題は、クラス テスト ケースのインスタンスごとに個別のログ ファイルを作成する必要があることです。たとえば、3 つのテスト ケース オブジェクトがあり、最終的には 3 つのログ ファイルを取得しますが、次のようになります。
テスト ケース #3 にはテスト ケース #3 のログが含まれ、テスト ケース #2 にはテスト ケース 2 および 3 のログが含まれ、テスト ケース #1 にはすべてのテスト ケースのログが含まれます。
これが私のコードです:
public class TestCase {
TestCase(String tcName){
this.tcName = tcName;
}
Logger log = Logger.getLogger("com.sigmaukraine.trn.autotest.testcase");
String tcName;
String scenarioReportDir;
List<Keyword> kwList = new ArrayList<Keyword>();
public void executeTestCase(){
//saving log for current test case
try {
FileHandler fh;
String fileName = new StringBuilder(tcName).append(".log").toString();
// This block configure the logger with handler and formatter
fh = new FileHandler(scenarioReportDir + fileName);
log.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
log.info("Executing test case: " + tcName);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for(Keyword k : kwList){
k.executeKeyword();
}
}