Skip to content

Commit

Permalink
Merge pull request #54 from tarpas/refactor
Browse files Browse the repository at this point in the history
Refactor.
  • Loading branch information
tarpas authored Dec 9, 2016
2 parents 00f94e9 + 2569a38 commit a40df87
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 160 deletions.
2 changes: 1 addition & 1 deletion exampleproject/tests/test_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_add(self):

def test_subtract(self):
"""test subtracting"""
self.assertEqual(subtract(1, 2), -1)
self.assertEqual(subtract(1 , 2), -1)

@pytest.mark.xfail(True, reason="Division by zero not implemented yet.")
def test_always_fail(self):
Expand Down
3 changes: 1 addition & 2 deletions exampleproject/tests_2/test_e.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
import pydevd;

@pytest.mark.skipif(True, reason="Because")
def test_1():
pass
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pytest>=2.7,<2.9
pytest>=2.8,<3.1
coverage>=4
2 changes: 2 additions & 0 deletions t.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
if __name__ == '__main__':
#test/test_testmon.py::TestmonDeselect::test_nonfunc_class
pytest.main("--tb=native -v" )
#pytest.main("-v -n 2 --tx=popen//python=python3")
#pytest.main("--help -v")
112 changes: 98 additions & 14 deletions test/test_core.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from collections import namedtuple

from testmon.process_code import Module
from test.test_process_code import CodeSample
from test.test_testmon import get_modules
from testmon.testmon_core import TestmonData as CoreTestmonData, flip_dictionary
from testmon.testmon_core import is_dependent, affected_nodeids
from testmon.testmon_core import TestmonData as CoreTestmonData, flip_dictionary, unaffected

pytest_plugins = "pytester",

Block = namedtuple('Block', 'checksums')


def test_write_data(testdir):
td = CoreTestmonData(testdir.tmpdir.strpath, 'V1')
Expand All @@ -26,13 +28,16 @@ def test_read_nonexistent(testdir):


def test_write_read_data2(testdir):
original = ({'a.py': 1.0}, {'n1': {'a.py': [1]}}, ['n1'])
n1_node_data = {'a.py': [1]}
original = ({'a.py': 1.0}, ['n1'])
td = CoreTestmonData(testdir.tmpdir.strpath, 'default')
td.mtimes, td.node_data, td.lastfailed = original
td.mtimes, td.lastfailed = original
td.write_data()
td.set_dependencies('n1', n1_node_data, )
td2 = CoreTestmonData(testdir.tmpdir.strpath, 'default')
td2.read_data()
assert original == (td2.mtimes, td2.node_data, td2.lastfailed)
assert td2.node_data['n1'] == n1_node_data
assert original == (td2.mtimes, td2.lastfailed)


class TestDepGraph():
Expand Down Expand Up @@ -67,11 +72,6 @@ def test_two_modules_combination2(self):
assert is_dependent({'a.py': [101, 102]}, changed_py_files) == False
assert is_dependent({'a.py': [101], 'b.py': [107]}, changed_py_files) == True

def test_two_modules_combination3(self):
changed_py_files = {'b.py': get_modules([103, 104])}
assert is_dependent('test_1', changed_py_files) == False
assert is_dependent('test_both', changed_py_files) == False

def test_classes_depggraph(self):
module1 = Module(CodeSample("""\
class TestA(object):
Expand Down Expand Up @@ -119,12 +119,48 @@ def test_affected_list(self):

assert set(td.file_data()) == set(['test_a.py', 'test_b.py'])

assert affected_nodeids(td.node_data, changes) == ['node1']
assert affected_nodeids(td.node_data, changes) == {'node1'}

def test_affected_list2(self):
changes = {'test_a.py': [102, 103]}
dependencies = {'node1': {'test_a.py': [102, 103, 104]}, }
assert affected_nodeids(dependencies, changes) == ['node1']
assert affected_nodeids(dependencies, changes) == {'node1'}


class TestUnaffected():
def test_nothing_changed(self):
changed = {'a.py': [101, 102, 103]}
dependencies = {'node1': {'test_a.py': [201, 202], 'a.py': [101, 102, 103]}}
assert unaffected(dependencies, blockify(changed))[0] == dependencies

def test_simple_change(self):
changed = {'a.py': [101, 102, 151]}
dependencies = {'node1': {'test_a.py': [201, 202], 'a.py': [101, 102, 103]},
'node2': {'test_b.py': [301, 302], 'a.py': [151]}}

nodes, files = unaffected(dependencies, blockify(changed))

assert set(nodes) == {'node2'}
assert set(files) == {'test_b.py'}


def get_modules(checksums):
return checksums


def is_dependent(dependencies, changes):
result = affected_nodeids({'testnode': dependencies}, changes)
return result == {'testnode'}


def affected_nodeids(dependencies, changes):
unaffected_nodes, files = unaffected(dependencies, blockify(changes))
return set(dependencies) - set(unaffected_nodes)


def blockify(changes):
block_changes = {key: Block(value) for key, value in changes.items()}
return block_changes


def test_variants_separation(testdir):
Expand All @@ -141,7 +177,55 @@ def test_variants_separation(testdir):
assert testmon1_data.node_data['node1'] == {'a.py': 1}


def test_flipp():
def test_flip():
node_data = {'X': {'a': [1, 2, 3], 'b': [3, 4, 5]}, 'Y': {'b': [3, 6, 7]}}
files = flip_dictionary(node_data)
assert files == {'a': {'X': [1, 2, 3]}, 'b': {'X': [3, 4, 5], 'Y': [3, 6, 7]}}


global_reports = []


def serialize_report(rep):
import py
d = rep.__dict__.copy()
if hasattr(rep.longrepr, 'toterminal'):
d['longrepr'] = str(rep.longrepr)
else:
d['longrepr'] = rep.longrepr
for name in d:
if isinstance(d[name], py.path.local):
d[name] = str(d[name])
elif name == "result":
d[name] = None # for now
return d


def test_serialize(testdir):
class PlugWrite:
def pytest_runtest_logreport(self, report):
global global_reports
global_reports.append(report)

class PlugRereport:
def pytest_runtest_protocol(self, item, nextitem):
hook = getattr(item.ihook, 'pytest_runtest_logreport')
for g in global_reports:
hook(report=g)
return True

testdir.makepyfile("""
def test_a():
raise Exception('exception from test_a')
""")

testdir.runpytest_inprocess(plugins=[PlugWrite()])

testdir.makepyfile("""
def test_a():
pass
""")

result = testdir.runpytest_inprocess(plugins=[PlugRereport()])

print(result)
3 changes: 0 additions & 3 deletions test/test_process_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@ def __init__(self, source_code, expected_coverage=None, possible_lines=None):
self.expected_coverage = expected_coverage or {}
self.possible_lines = possible_lines or []

def line_count(self):
return len(self.source_code.splitlines())


code_samples = {
1: CodeSample("""\
Expand Down
5 changes: 3 additions & 2 deletions test/test_subprocess.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import pytest
from test.coveragepy.coveragetest import CoverageTest

Expand All @@ -8,7 +9,7 @@ def test_basic_run(self):
path1 = self.make_file("subprocesstest.py", """\
print("hello world")
""")
output = self.run_command('python {}'.format(path1))
output = self.run_command('{} {}'.format(sys.executable, path1))
assert output == "hello world\n"

def test_pass_environ(self):
Expand All @@ -17,7 +18,7 @@ def test_pass_environ(self):
print(os.environ['TEST_NAME'])
""")
os.environ['TEST_NAME'] = 'TEST_VALUE'
output = self.run_command('python {}'.format(path1))
output = self.run_command('{} {}'.format(sys.executable, path1))
assert output == "{}\n".format('TEST_VALUE')

@pytest.mark.xfail
Expand Down
Loading

0 comments on commit a40df87

Please sign in to comment.