How to use testSomething method in Ddt

Best Python code snippet using ddt_python

result_sink_util_test.py

Source:result_sink_util_test.py Github

copy

Full Screen

1#!/usr/bin/env vpython2# Copyright 2020 The Chromium Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5import base646import json7import mock8import requests9import unittest10import result_sink_util11SINK_ADDRESS = 'sink/address'12SINK_POST_URL = 'http://%s/prpc/luci.resultsink.v1.Sink/ReportTestResults' % SINK_ADDRESS13AUTH_TOKEN = 'some_sink_token'14LUCI_CONTEXT_FILE_DATA = """15{16 "result_sink": {17 "address": "%s",18 "auth_token": "%s"19 }20}21""" % (SINK_ADDRESS, AUTH_TOKEN)22HEADERS = {23 'Content-Type': 'application/json',24 'Accept': 'application/json',25 'Authorization': 'ResultSink %s' % AUTH_TOKEN26}27class UnitTest(unittest.TestCase):28 def test_compose_test_result(self):29 """Tests compose_test_result function."""30 # Test a test result without log_path.31 test_result = result_sink_util._compose_test_result(32 'TestCase/testSomething', 'PASS', True)33 expected = {34 'testId': 'TestCase/testSomething',35 'status': 'PASS',36 'expected': True,37 'tags': [],38 'testMetadata': {39 'name': 'TestCase/testSomething'40 },41 }42 self.assertEqual(test_result, expected)43 short_log = 'Some logs.'44 # Tests a test result with log_path.45 test_result = result_sink_util._compose_test_result(46 'TestCase/testSomething',47 'PASS',48 True,49 test_log=short_log,50 duration=1233,51 file_artifacts={'name': '/path/to/name'})52 expected = {53 'testId': 'TestCase/testSomething',54 'status': 'PASS',55 'expected': True,56 'summaryHtml': '<text-artifact artifact-id="Test Log" />',57 'artifacts': {58 'Test Log': {59 'contents':60 base64.b64encode(short_log.encode('utf-8')).decode('utf-8')61 },62 'name': {63 'filePath': '/path/to/name'64 },65 },66 'duration': '1.233000000s',67 'tags': [],68 'testMetadata': {69 'name': 'TestCase/testSomething'70 },71 }72 self.assertEqual(test_result, expected)73 def test_long_test_log(self):74 """Tests long test log is reported as expected."""75 len_32_str = 'This is a string in length of 32'76 self.assertEqual(len(len_32_str), 32)77 len_4128_str = (4 * 32 + 1) * len_32_str78 self.assertEqual(len(len_4128_str), 4128)79 expected = {80 'testId': 'TestCase/testSomething',81 'status': 'PASS',82 'expected': True,83 'summaryHtml': '<text-artifact artifact-id="Test Log" />',84 'artifacts': {85 'Test Log': {86 'contents':87 base64.b64encode(len_4128_str.encode('utf-8')88 ).decode('utf-8')89 },90 },91 'tags': [],92 'testMetadata': {93 'name': 'TestCase/testSomething'94 },95 }96 test_result = result_sink_util._compose_test_result(97 'TestCase/testSomething', 'PASS', True, test_log=len_4128_str)98 self.assertEqual(test_result, expected)99 def test_compose_test_result_assertions(self):100 """Tests invalid status is rejected"""101 with self.assertRaises(AssertionError):102 test_result = result_sink_util._compose_test_result(103 'TestCase/testSomething', 'SOME_INVALID_STATUS', True)104 with self.assertRaises(AssertionError):105 test_result = result_sink_util._compose_test_result(106 'TestCase/testSomething', 'PASS', True, tags=('a', 'b'))107 with self.assertRaises(AssertionError):108 test_result = result_sink_util._compose_test_result(109 'TestCase/testSomething',110 'PASS',111 True,112 tags=[('a', 'b', 'c'), ('d', 'e')])113 with self.assertRaises(AssertionError):114 test_result = result_sink_util._compose_test_result(115 'TestCase/testSomething', 'PASS', True, tags=[('a', 'b'), ('c', 3)])116 def test_composed_with_tags(self):117 """Tests tags is in correct format."""118 expected = {119 'testId': 'TestCase/testSomething',120 'status': 'SKIP',121 'expected': True,122 'tags': [{123 'key': 'disabled_test',124 'value': 'true',125 }],126 'testMetadata': {127 'name': 'TestCase/testSomething'128 },129 }130 test_result = result_sink_util._compose_test_result(131 'TestCase/testSomething',132 'SKIP',133 True,134 tags=[('disabled_test', 'true')])135 self.assertEqual(test_result, expected)136 @mock.patch.object(requests.Session, 'post')137 @mock.patch('%s.open' % 'result_sink_util',138 mock.mock_open(read_data=LUCI_CONTEXT_FILE_DATA))139 @mock.patch('os.environ.get', return_value='filename')140 def test_post_test_result(self, mock_open_file, mock_session_post):141 test_result = {142 'testId': 'TestCase/testSomething',143 'status': 'SKIP',144 'expected': True,145 'tags': [{146 'key': 'disabled_test',147 'value': 'true',148 }],149 'testMetadata': {150 'name': 'TestCase/testSomething'151 },152 }153 client = result_sink_util.ResultSinkClient()154 client._post_test_result(test_result)155 mock_session_post.assert_called_with(156 url=SINK_POST_URL,157 headers=HEADERS,158 data=json.dumps({'testResults': [test_result]}))159 @mock.patch.object(requests.Session, 'close')160 @mock.patch.object(requests.Session, 'post')161 @mock.patch('%s.open' % 'result_sink_util',162 mock.mock_open(read_data=LUCI_CONTEXT_FILE_DATA))163 @mock.patch('os.environ.get', return_value='filename')164 def test_close(self, mock_open_file, mock_session_post, mock_session_close):165 client = result_sink_util.ResultSinkClient()166 client._post_test_result({'some': 'result'})167 mock_session_post.assert_called()168 client.close()169 mock_session_close.assert_called()170 def test_post(self):171 client = result_sink_util.ResultSinkClient()172 client.sink = 'Make sink not None so _compose_test_result will be called'173 client._post_test_result = mock.MagicMock()174 client.post(175 'testname',176 'PASS',177 True,178 test_log='some_log',179 tags=[('tag key', 'tag value')])180 client._post_test_result.assert_called_with(181 result_sink_util._compose_test_result(182 'testname',183 'PASS',184 True,185 test_log='some_log',186 tags=[('tag key', 'tag value')]))187 client.post('testname', 'PASS', True, test_log='some_log')188 client._post_test_result.assert_called_with(189 result_sink_util._compose_test_result(190 'testname', 'PASS', True, test_log='some_log'))191 client.post('testname', 'PASS', True)192 client._post_test_result.assert_called_with(193 result_sink_util._compose_test_result('testname', 'PASS', True))194if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Ddt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful