2016-08-23 7 views
0

C#에서 Selenium Webdriver를 사용하여 테스트 용 사용자 지정 결과 파일을 만들려고합니다.테스트 실패시 결과 파일을 닫으려면 어떻게합니까? Webdriver 사용, C#

결과를 CSV 파일에 쓰고 끝에서 닫습니다.

문제는 테스트가 실패하고 완료되지 않으면 파일이 절대로 닫히지 않아 결과가 나타나지 않는다는 것입니다.

필자는 file.Close(); Teardown 섹션에 있지만 "파일"이 해당 컨텍스트에 없기 때문에 작동하지 않습니다. 내가 그것을 전달하는 방법을 볼 수 없습니다.

또한 설치 프로그램에서 새로운 StreamWriter 파일 설정을 시도했는데, 문제가 없지만 끝까지 닫을 수는 없었습니다.

여기서는 일반적인 Google 검색을 검색했습니다.

다음은 작동하는 샘플입니다 (모두 한 곳에서 테스트 할 때 다른 클래스).

나는 file.Close()를 움직이기를 원한다. 통과 여부에 관계없이 어디로 달릴 것인가.

[TearDown] 
    public void TeardownTest() 
    { 
     try 
     { 
      driver.Quit(); 
     } 
     catch (Exception) 
     { 
      // Ignore errors if unable to close the browser 
     } 
     Assert.AreEqual("", verificationErrors.ToString()); 

    // file.Close(); 
    // this is where it doesn't work if I put it here 
    } 

    [Test] 
    public void TheTest() 
    {   
     System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\results\test.csv", true); 

     try 
     { 
      Assert.AreEqual("text", "text"); 
      file.WriteLine("{0},{1},{2}", "time", "test", "PASS"); 
     } 
     catch (AssertionException e) 
     { 
      verificationErrors.Append(e.Message); 
      file.WriteLine("{0},{1},{2}", "time", "test", "FAIL"); 
     } 

     //do next step 

     file.Close(); 
    } 
+1

을 노력이 클래스의 모든 메소드 – Siva

+0

감사합니다 사용할 수 있도록, class' 수준 '에서 공개로'file'를 선언하십시오 - 완벽했다! – Akcipitrokulo

답변

0

개체를 처분하지 않습니다. 사용하여 문 https://msdn.microsoft.com/en-GB/library/yh598w02.aspx

[Test] 
    public void TheTest() 
    {   
     using(System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\results\test.csv", true)) 
{ 

     try 
     { 
      Assert.AreEqual("text", "text"); 
      file.WriteLine("{0},{1},{2}", "time", "test", "PASS"); 
     } 
     catch (AssertionException e) 
     { 
      verificationErrors.Append(e.Message); 
      file.WriteLine("{0},{1},{2}", "time", "test", "FAIL"); 
     } 

     //do next step 

     } 
    }