Best Python code snippet using ddt_python
result_sink_util_test.py
Source: result_sink_util_test.py
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__':...
Check out the latest blogs from LambdaTest on this topic:
“Test frequently and early.” If you’ve been following my testing agenda, you’re probably sick of hearing me repeat that. However, it is making sense that if your tests detect an issue soon after it occurs, it will be easier to resolve. This is one of the guiding concepts that makes continuous integration such an effective method. I’ve encountered several teams who have a lot of automated tests but don’t use them as part of a continuous integration approach. There are frequently various reasons why the team believes these tests cannot be used with continuous integration. Perhaps the tests take too long to run, or they are not dependable enough to provide correct results on their own, necessitating human interpretation.
Even though several frameworks are available in the market for automation testing, Selenium is one of the most renowned open-source frameworks used by experts due to its numerous features and benefits.
In my last blog, I investigated both the stateless and the stateful class of model-based testing. Both have some advantages and disadvantages. You can use them for different types of systems, depending on whether a stateful solution is required or a stateless one is enough. However, a better solution is to use an aggregate technique that is appropriate for each system. Currently, the only aggregate solution is action-state testing, introduced in the book Paradigm Shift in Software Testing. This method is implemented in Harmony.
People love to watch, read and interact with quality content — especially video content. Whether it is sports, news, TV shows, or videos captured on smartphones, people crave digital content. The emergence of OTT platforms has already shaped the way people consume content. Viewers can now enjoy their favorite shows whenever they want rather than at pre-set times. Thus, the OTT platform’s concept of viewing anything, anytime, anywhere has hit the right chord.
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!