def test_matplot(self, tmpdir): result_dir = str(tmpdir) result = result_mod.Result(_scratch=result_dir) x = range(0, 10) y = range(0, 10) plot.plot(x, y) result.matplot(plot, description="Matplot") assert len(result.entries) == 1 assert len(result.attachments) == 1 with result.group(description="subgroup") as subgroup: x = range(0, 10) y = range(0, 10) plot.plot(x, y) subgroup.matplot(plot, description="Matplot") assert len(result.entries) == 2 assert len(result.attachments) == 2 # two different file, with same content on the same directory assert (result.attachments[0].source_path != result.attachments[1].source_path) assert result.attachments[0].hash == result.attachments[1].hash assert result.attachments[0].source_path.startswith(result_dir) assert result.attachments[1].source_path.startswith(result_dir)
def test_attach_in_result_group(self, tmpdir): """UT for result.attach method.""" tmpfile = str(tmpdir.join("attach_me.txt")) with open(tmpfile, "w") as f: f.write("testplan\n" * 1000) size = os.path.getsize(tmpfile) description = "Attach a text file at level: {}" result = result_mod.Result(_scratch=str(tmpdir)) assert result.attach(tmpfile, description=description.format(0)) assert len(result.entries) == 1 with result.group("subgroup") as subgroup: assert subgroup.attach(tmpfile, description=description.format(1)) assert len(subgroup.entries) == 1 with subgroup.group("subgroup") as subsubgroup: assert subsubgroup.attach(tmpfile, description=description.format(2)) assert len(subsubgroup.entries) == 1 assert len(subgroup.entries) == 2 assert len(subgroup.attachments) == 2 assert len(result.entries) == 2 assert len(result.attachments) == 3 for idx, attachment in enumerate(result.attachments): assert attachment.source_path == os.path.join( os.path.dirname(tmpfile), attachment.dst_path) assert attachment.orig_filename == "attach_me.txt" assert attachment.filesize == size assert attachment.description == description.format(idx)
def test_group_marking(): """ Tests, at result object level, if marking works as expected. """ result = result_mod.Result() result.equal(1, 1) assert result.entries.pop().line_no == get_line_no(test_group_marking, 5) helper(result) assert result.entries.pop().line_no == get_line_no(helper, 1) intermediary(result) assert result.entries.pop().line_no == get_line_no(intermediary, 2)
def test_attach_dir(self, tmpdir): """UT for result.attach method.""" path_utils.makeemptydirs(str(tmpdir.join("subdir"))) tmpfile1 = str(tmpdir.join("1.txt")) with open(tmpfile1, "w") as f: f.write("testplan\n" * 10) tmpfile2 = str(tmpdir.join("2.txt")) with open(tmpfile2, "w") as f: f.write("testplan\n") tmpfile3 = str(tmpdir.join("subdir").join("3.txt")) with open(tmpfile3, "w") as f: f.write("testplan\n" * 100) tmpfile4 = str(tmpdir.join("subdir").join("4.txt")) with open(tmpfile4, "w") as f: f.write("testplan\n" * 1000) result = result_mod.Result() assert result.attach(str(tmpdir), description="Attach a directory") assert len(result.entries) == 1 directory_entry = result.entries[0] assert directory_entry.source_path == str(tmpdir) assert (directory_entry.dst_path == hashlib.md5( directory_entry.source_path.encode("utf-8")).hexdigest()) assert sorted(directory_entry.file_list) == ["1.txt", "2.txt"] assert result.attach( str(tmpdir), description="Attach a directory with filters", ignore=["2.*"], only=["*.txt"], recursive=True, ) assert len(result.entries) == 2 directory_entry = result.entries[1] assert directory_entry.source_path == str(tmpdir) assert (directory_entry.dst_path == hashlib.md5( directory_entry.source_path.encode("utf-8")).hexdigest()) assert sorted([ file.replace("\\", "/") for file in directory_entry.file_list ]) == [ "1.txt", "subdir/3.txt", "subdir/4.txt", ]
def test_graph_assertion(self): """Unit testcases for the result.graph class.""" result = result_mod.Result() graph_assertion = result.graph( 'Line', { 'Data Name': [{ 'x': 0, 'y': 8 }, { 'x': 1, 'y': 5 }, { 'x': 2, 'y': 4 }, { 'x': 3, 'y': 9 }, { 'x': 4, 'y': 1 }, { 'x': 5, 'y': 7 }, { 'x': 6, 'y': 6 }, { 'x': 7, 'y': 3 }, { 'x': 8, 'y': 2 }, { 'x': 9, 'y': 0 }] }, description='Line Graph', series_options={'Data Name': { "colour": "red" }}, graph_options=None) assert graph_assertion is True assert len(result.entries) == 1 assert result.entries[0].graph_type is 'Line' assert type(result.entries[0].graph_data) is dict assert type(result.entries[0].series_options) is dict assert result.entries[0].graph_options is None
def test_attach(self, tmpdir): """UT for result.attach method.""" tmpfile = str(tmpdir.join("attach_me.txt")) with open(tmpfile, "w") as f: f.write("testplan\n" * 1000) result = result_mod.Result() assert result.attach(tmpfile, description="Attach a text file") assert len(result.entries) == 1 attachment_entry = result.entries[0] assert attachment_entry.source_path == tmpfile assert attachment_entry.hash == path_utils.hash_file(tmpfile) assert attachment_entry.orig_filename == "attach_me.txt" assert attachment_entry.filesize == os.path.getsize(tmpfile) # The expected destination path depends on the exact hash and filesize # of the file we wrote. expected_dst_path = "attach_me-{hash}-{filesize}.txt".format( hash=attachment_entry.hash, filesize=attachment_entry.filesize) assert attachment_entry.dst_path == expected_dst_path
def test_bool(self): result = result_mod.Result() assert result assert len(result) == 0 assert result.passed first = result.subresult() second = result.subresult() first.true(True, "AssertionFirst") second.true(True, "AssertionSecond") result.append(first) result.append(second) assert len(result) == 2 assert result.passed third = result.subresult() third.true(False, "AssertionThird") result.append(third) assert len(result) == 3 assert not result.passed
def test_graph_assertion(self): """Unit testcase for the result.graph method.""" result = result_mod.Result() graph_assertion = result.graph( "Line", { "Data Name": [ { "x": 0, "y": 8 }, { "x": 1, "y": 5 }, { "x": 2, "y": 4 }, { "x": 3, "y": 9 }, { "x": 4, "y": 1 }, { "x": 5, "y": 7 }, { "x": 6, "y": 6 }, { "x": 7, "y": 3 }, { "x": 8, "y": 2 }, { "x": 9, "y": 0 }, ] }, description="Line Graph", series_options={"Data Name": { "colour": "red" }}, graph_options=None, ) assert bool(graph_assertion) is True assert len(result.entries) == 1 assert result.entries[0].graph_type is "Line" assert type(result.entries[0].graph_data) is dict assert type(result.entries[0].series_options) is dict assert result.entries[0].graph_options is None