Skip to content

Commit

Permalink
Testing: Add all run code tests to a single test
Browse files Browse the repository at this point in the history
This greatly reduces test time for this functionality
  • Loading branch information
ccordoba12 committed Feb 9, 2017
1 parent 6737a34 commit d61f97e
Showing 1 changed file with 35 additions and 39 deletions.
74 changes: 35 additions & 39 deletions spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ def open_file_in_editor(main_window, fname, directory=None):
QTest.keyClick(w, Qt.Key_Enter)


def reset_run_code(qtbot, shell, code_editor, nsb):
"""Reset state after a run code test"""
shell.execute('%reset -f')
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 0, timeout=1500)
code_editor.setFocus()
qtbot.keyClick(code_editor, Qt.Key_Home, modifier=Qt.ControlModifier)


#==============================================================================
# Fixtures
#==============================================================================
Expand All @@ -54,11 +62,15 @@ def main_window():
return widget


@pytest.fixture
def run_code(main_window, qtbot):
#==============================================================================
# Tests
#==============================================================================
def test_run_code(main_window, qtbot):
"""Test all the different ways we have to run code"""
# ---- Setup ----
# Wait until the window is fully up
shell = main_window.ipyconsole.get_current_shellwidget()
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=6000)
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=15000)

# Load test file
main_window.editor.load(osp.join(LOCATION, 'script.py'))
Expand All @@ -71,75 +83,57 @@ def run_code(main_window, qtbot):
# Get a reference to the namespace browser widget
nsb = main_window.variableexplorer.get_focus_widget()

yield shell, code_editor, nsb

main_window.close()


#==============================================================================
# Tests
#==============================================================================
def test_run_file(main_window, qtbot, run_code):
"""Test that running a file line by line is working"""
shell, code_editor, nsb = run_code

# Run file
# ---- Run file ----
qtbot.keyClick(code_editor, Qt.Key_F5)

# Wait until all objects have appeared in the variable explorer
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 3, timeout=500)
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 3, timeout=1500)

# Verify result
assert shell.get_value('a') == 10
assert shell.get_value('li') == [1, 2, 3]
assert_array_equal(shell.get_value('arr'), np.array([1, 2, 3]))

reset_run_code(qtbot, shell, code_editor, nsb)

def test_run_lines(main_window, qtbot, run_code):
"""Test that running a file line by line is working"""
shell, code_editor, nsb = run_code

# ---- Run lines ----
# Run the whole file line by line
for _ in range(code_editor.blockCount()):
qtbot.keyClick(code_editor, Qt.Key_F9)
qtbot.wait(100)

# Wait until all objects have appeared in the variable explorer
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 3, timeout=500)
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 3, timeout=1500)

# Verify result
assert shell.get_value('a') == 10
assert shell.get_value('li') == [1, 2, 3]
assert_array_equal(shell.get_value('arr'), np.array([1, 2, 3]))

reset_run_code(qtbot, shell, code_editor, nsb)

def test_run_cell_and_advance(main_window, qtbot, run_code):
"""Test that running multiple cells is working"""
shell, code_editor, nsb = run_code

# ---- Run cell and advance ----
# Run the three cells present in file
for _ in range(3):
qtbot.keyClick(code_editor, Qt.Key_Return, modifier=Qt.ShiftModifier)
qtbot.wait(100)

# Wait until all objects have appeared in the variable explorer
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 3, timeout=500)
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 3, timeout=1500)

# Verify result
assert shell.get_value('a') == 10
assert shell.get_value('li') == [1, 2, 3]
assert_array_equal(shell.get_value('arr'), np.array([1, 2, 3]))

reset_run_code(qtbot, shell, code_editor, nsb)

def test_run_cell(main_window, qtbot, run_code):
"""Test that running a single cell is working"""
shell, code_editor, nsb = run_code

# ---- Run cell ----
# Run the first cell in file
qtbot.keyClick(code_editor, Qt.Key_Return, modifier=Qt.ControlModifier)

# Wait until the object has appeared in the variable explorer
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 1, timeout=500)
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 1, timeout=1500)

# Verify result
assert shell.get_value('a') == 10
Expand All @@ -149,6 +143,8 @@ def test_run_cell(main_window, qtbot, run_code):
qtbot.keyClick(code_editor, Qt.Key_Return, modifier=Qt.ControlModifier)
assert nsb.editor.model.rowCount() == 1

main_window.close()


def test_open_files_in_new_editor_window(main_window, qtbot):
"""
Expand All @@ -159,7 +155,7 @@ def test_open_files_in_new_editor_window(main_window, qtbot):
"""
# Wait until the window is fully up
shell = main_window.ipyconsole.get_current_shellwidget()
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=6000)
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=15000)

# Set a timer to manipulate the open dialog while it's running
QTimer.singleShot(2000, lambda: open_file_in_editor(main_window,
Expand All @@ -183,7 +179,7 @@ def test_maximize_minimize_plugins(main_window, qtbot):
"""Test that the maximize button is working correctly."""
# Wait until the window is fully up
shell = main_window.ipyconsole.get_current_shellwidget()
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=6000)
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=15000)

# Set focus to the Editor
main_window.editor.get_focus_widget().setFocus()
Expand Down Expand Up @@ -214,12 +210,12 @@ def test_issue_4066(main_window, qtbot):
"""
# Create the object
shell = main_window.ipyconsole.get_current_shellwidget()
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=6000)
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=15000)
shell.execute('myobj = [1, 2, 3]')

# Open editor associated with that object and get a reference to it
nsb = main_window.variableexplorer.get_focus_widget()
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() > 0, timeout=500)
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() > 0, timeout=1500)
nsb.editor.setFocus()
nsb.editor.edit_item()
obj_editor_id = list(nsb.editor.delegate._editors.keys())[0]
Expand All @@ -228,7 +224,7 @@ def test_issue_4066(main_window, qtbot):
# Move to the IPython console and delete that object
main_window.ipyconsole.get_focus_widget().setFocus()
shell.execute('del myobj')
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 0, timeout=500)
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 0, timeout=1500)

# Close editor
ok_widget = obj_editor.bbox.button(obj_editor.bbox.Ok)
Expand All @@ -246,13 +242,13 @@ def test_varexp_edit_inline(main_window, qtbot):
"""
# Create object
shell = main_window.ipyconsole.get_current_shellwidget()
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=6000)
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=15000)
shell.execute('a = 10')

# Edit object
main_window.variableexplorer.visibility_changed(True)
nsb = main_window.variableexplorer.get_focus_widget()
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() > 0, timeout=500)
qtbot.waitUntil(lambda: nsb.editor.model.rowCount() > 0, timeout=1500)
nsb.editor.setFocus()
nsb.editor.edit_item()

Expand Down

0 comments on commit d61f97e

Please sign in to comment.