'Given a test failure how can you extract all code statements that the test execution ran?

Given a test failure, I have to extract all code statements that the test execution ran.

Let's say unit test 1 has failed. I have to extract all the codes it executed.

public class Driver {

  method1 {
  }  

  method2 {
  }  

  method3 {
  }  

  public TakeScreenshot(int flag){
     statement1;
     statement2;
     
     if(flag) {
       statement_inside_flag;
     }
    

     statement100;
  }
}
[TestMethod]
public void TestThings()
{
        boolean result = Driver.TakeScreenshot(true);
        Assert.isTrue(result);
}

Is there an easy way I can do it using an open-source tool?

If I want to extract the body of the code under test i.e. in this case the output would be as follows. Some lines in the TakeScreenshot was not executed:

public class Driver {
  public TakeScreenshot(int flag){
     statement1;
     statement2;
     
     if(flag) {
       statement_inside_flag;
       return;
     }
  }
}


Solution 1:[1]

If you check the page source you will see that class is not present. Instead, you can use an attribute selector as follows:

import requests
from bs4 import BeautifulSoup as bs

r = requests.get('http://www.bcra.gob.ar//PublicacionesEstadisticas/Principales_variables_datos_i.asp?serie=246&detalle=BCRA', headers = {'User-Agent':'Mozilla/5.0'})
soup = bs(r.content, 'lxml')
print(soup.select_one('[max]')['max'])

Solution 2:[2]

You can try to use the Webdriver from Selenium to first get the "full" source code. It seems like the requests object does not have the fully rendered html code. This may be due to certain scripts that are not "loaded" with the requests approach:

from bs4 import BeautifulSoup
from selenium import webdriver

link = 'http://www.bcra.gob.ar//PublicacionesEstadisticas/Principales_variables_datos_i.asp?serie=7935&detalle=Monetary Policy Rate (APR %)'

#Download the chrome drivers and provide their location instead of "XYZ"
driver = webdriver.Chrome(executable_path=r'C:\XYZ\chromedriver.exe')
driver.get(link)

#Now you can access the code rendered by the Chrome webdriver: 
soup_direct_link = BeautifulSoup(str(driver.page_source), 'lxml')

max_start_date = soup_direct_link.find('form', class_ = 'form-inline').input["max"]
print(max_start_date)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 QHarr
Solution 2 Carst3n