<jupyter_start><jupyter_text>Script for Integration of Slack with Pytest Framework ( Dynamic Report Generation on Slack App )<jupyter_code>#Contents of post_test_reports_to_slack.py 
import json,os,requests
 
def post_reports_to_slack():
 
        url= "Put your Slack incoming webhook url here"  
 
        #To generate report file add "> pytest_report.log" at end of py.test command for e.g. py.test -v > pytest_report.log
        test_report_file = os.path.abspath(os.path.join(os.path.dirname(__file__),'pytest_report.log')) #Add report file name and address here
 
        # Open report file and read data
        with open(test_report_file, "r") as in_file:
                testdata = ""
                for line in in_file:
                        testdata = testdata + '\n' + line
 
        # Set Slack Pass Fail bar indicator color according to test results   
        if 'FAILED' in testdata:
            bar_color = "#ff0000"
        else:
            bar_color = "#36a64f"
 
        # Arrange your data in pre-defined format. Test your data format here: https://api.slack.com/docs/messages/builder?  
        data = {"attachments":[
                            {"color": bar_color,
                            "title": "Test Report",
                            "text": testdata}
                            ]}
        json_params_encoded = json.dumps(data)
        slack_response = requests.post(url=url,data=json_params_encoded,headers={"Content-type":"application/json"})
        if slack_response.text == 'ok':
                print '\n Successfully posted pytest report on Slack channel'
        else:
                print '\n Something went wrong. Unable to post pytest report on Slack channel. Slack Response:', slack_response 
 
#---USAGE EXAMPLES
if __name__=='__main__':
        post_reports_to_slack()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09056603773584905, 'fraction_numerical': 0.00431266846361186, 'mean_word_length': 2.2222222222222223, 'pattern_counts': {'":': 5, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '445393', 'license': 'MIT', 'n_tokens_mistral': 553, 'n_tokens_neox': 524, 'n_words': 153, 'path': 'Section 4.1 Slack Integration for Pytest Report.ipynb', 'repo_name': 'Secure-intelligent-agent/Automation-test-engineer', 'size': '1855'}