Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions autoload/vim_python_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,36 @@ def get_command_to_run_current_base_method_with_nosetests(path_to_current_file,
return command


def get_command_to_run_current_file_with_pytests(path_to_current_file):
command = ":!pytest {0}".format(path_to_current_file)
write_test_command_to_cache_file(command)
return command


def get_command_to_run_current_class_with_pytests(path_to_current_file, current_line, current_buffer):
run_file = get_command_to_run_current_file_with_pytests(path_to_current_file)
current_class = get_current_method_and_class(current_line, current_buffer)[0]
command = run_file + "::" + current_class
write_test_command_to_cache_file(command)
return command


def get_command_to_run_current_method_with_pytests(path_to_current_file, current_line, current_buffer):
run_class = get_command_to_run_current_class_with_pytests(path_to_current_file, current_line, current_buffer)
current_method = get_current_method_and_class(current_line, current_buffer)[1]
command = run_class + "::" + current_method
write_test_command_to_cache_file(command)
return command


def get_command_to_run_current_base_method_with_pytests(path_to_current_file, current_line, current_buffer):
run_file = get_command_to_run_current_file_with_pytests(path_to_current_file)
current_method = get_current_method_and_class(current_line, current_buffer)[1]
command = run_file + "::" + current_method
write_test_command_to_cache_file(command)
return command


def get_command_to_rerun_last_tests():
with open("/tmp/vim_python_test_runner_cache", "r") as f:
return f.read()
Expand Down
7 changes: 7 additions & 0 deletions autoload/vim_python_test_runner.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def get_proper_command(desired_command, current_directory):
"nose_class": lambda: get_command_to_run_current_class_with_nosetests(vim.current.buffer.name, current_line_index, vim.current.buffer),
"nose_method": lambda: get_command_to_run_current_method_with_nosetests(vim.current.buffer.name, current_line_index, vim.current.buffer),
"nose_base_method": lambda: get_command_to_run_current_base_method_with_nosetests(vim.current.buffer.name, current_line_index, vim.current.buffer),
"pytest_file": lambda: get_command_to_run_current_file_with_pytests(vim.current.buffer.name),
"pytest_class": lambda: get_command_to_run_current_class_with_pytests(vim.current.buffer.name, current_line_index, vim.current.buffer),
"pytest_method": lambda: get_command_to_run_current_method_with_pytests(vim.current.buffer.name, current_line_index, vim.current.buffer),
"pytest_base_method": lambda: get_command_to_run_current_base_method_with_pytests(vim.current.buffer.name, current_line_index, vim.current.buffer),
"rerun": lambda: get_command_to_rerun_last_tests()
}
return FUNCTIONS[desired_command]()
Expand All @@ -36,6 +40,9 @@ def run_desired_command_for_os(command_to_run):
if "nose" in vim.eval("a:command_to_run") or "nose" in command_to_run:
# Run nosetests for Python.
vim.command("{0} 2>&1 | tee /tmp/test_results.txt".format(command_to_run))
if "pytest" in vim.eval("a:command_to_run") or "pytest" in command_to_run:
# Run pytests for Python.
vim.command("{0} 2>&1 | tee /tmp/test_results.txt".format(command_to_run))
else:
# Run manage.py test for Django.
vim.command(":!python {0} 2>&1 | tee /tmp/test_results.txt".format(command_to_run))
Expand Down
4 changes: 4 additions & 0 deletions doc/vim-python-test-runner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ your vimrc:
nnoremap<Leader>nc :NosetestClass<CR>
nnoremap<Leader>nm :NosetestMethod<CR>
nnoremap<Leader>nb :NosetestBaseMethod<CR>
nnoremap<Leader>tf :PytestFile<CR>
nnoremap<Leader>tc :PytestClass<CR>
nnoremap<Leader>tm :PytestMethod<CR>
nnoremap<Leader>tb :PytestBaseMethod<CR>
nnoremap<Leader>rr :RerunLastTests<CR>

Your tests results will be available in the quickfix window after they finish
Expand Down
4 changes: 4 additions & 0 deletions plugin/vim_python_test_runner.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ command! NosetestFile call vim_python_test_runner#RunDesiredTests("nose_file")
command! NosetestClass call vim_python_test_runner#RunDesiredTests("nose_class")
command! NosetestMethod call vim_python_test_runner#RunDesiredTests("nose_method")
command! NosetestBaseMethod call vim_python_test_runner#RunDesiredTests("nose_base_method")
command! PytestFile call vim_python_test_runner#RunDesiredTests("pytest_file")
command! PytestClass call vim_python_test_runner#RunDesiredTests("pytest_class")
command! PytestMethod call vim_python_test_runner#RunDesiredTests("pytest_method")
command! PytestBaseMethod call vim_python_test_runner#RunDesiredTests("pytest_base_method")
command! RerunLastTests call vim_python_test_runner#RunDesiredTests("rerun")
68 changes: 68 additions & 0 deletions tests/test_run_pytest_in_vim_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import os
import unittest
from inspect import getfile, currentframe

import autoload.vim_python_test_runner as sut


class RunPytestsInVimTests(unittest.TestCase):

def test_create_command_to_run_current_file_with_pytests(self):
path_to_current_file = "/tmp/project/tests/aTestFile.py"
command_to_run = sut.get_command_to_run_current_file_with_pytests(path_to_current_file)
self.assertEqual(":!pytest /tmp/project/tests/aTestFile.py", command_to_run)

def test_create_command_to_run_current_file_with_pytests_writes_command_to_cache_file_when_successfully_called(self):
path_to_current_file = "/tmp/project/tests/aTestFile.py"
command_to_run = sut.get_command_to_run_current_file_with_pytests(path_to_current_file)
last_command = sut.get_command_to_rerun_last_tests()
self.assertEqual(command_to_run, last_command)

def test_create_command_to_run_current_class_with_pytests(self):
path_to_current_file = "/tmp/project/tests/aTestFile.py"
current_line = 27
current_buffer = self.build_buffer_helper()
command_to_run = sut.get_command_to_run_current_class_with_pytests(path_to_current_file, current_line, current_buffer)
self.assertEqual(":!pytest /tmp/project/tests/aTestFile.py::Example2", command_to_run)

def test_create_command_to_run_current_class_with_pytests_writes_command_to_cache_file_when_successfully_called(self):
path_to_current_file = "/tmp/project/tests/aTestFile.py"
current_line = 27
current_buffer = self.build_buffer_helper()
command_to_run = sut.get_command_to_run_current_class_with_pytests(path_to_current_file, current_line, current_buffer)
last_command = sut.get_command_to_rerun_last_tests()
self.assertEqual(command_to_run, last_command)

def test_create_command_to_run_current_method_with_pytests(self):
path_to_current_file = "/tmp/project/tests/aTestFile.py"
current_line = 44
current_buffer = self.build_buffer_helper()
command_to_run = sut.get_command_to_run_current_method_with_pytests(path_to_current_file, current_line, current_buffer)
self.assertEqual(":!pytest /tmp/project/tests/aTestFile.py::Example3::double_dummy1", command_to_run)

def test_create_command_to_run_current_method_with_pytests_writes_command_to_cache_file_when_successfully_called(self):
path_to_current_file = "/tmp/project/tests/aTestFile.py"
current_line = 44
current_buffer = self.build_buffer_helper()
command_to_run = sut.get_command_to_run_current_method_with_pytests(path_to_current_file, current_line, current_buffer)
last_command = sut.get_command_to_rerun_last_tests()
self.assertEqual(command_to_run, last_command)

def test_create_command_to_run_current_method_with_pytests_when_not_in_a_class(self):
path_to_current_file = "/tmp/project/tests/aTestFile.py"
current_line = 5
current_buffer = self.build_buffer_helper("classless_dummy_file.py")
command_to_run = sut.get_command_to_run_current_base_method_with_pytests(path_to_current_file, current_line, current_buffer)
self.assertEqual(":!pytest /tmp/project/tests/aTestFile.py::dummy_base_method1", command_to_run)

def build_buffer_helper(self, dummy_file="dummy_test_file.py"):
current_dir = os.path.dirname(os.path.abspath(getfile(currentframe())))
with open("{}/{}".format(current_dir, dummy_file), "r") as f:
current_buffer = []
for line in f.readlines():
current_buffer.append(line)
return current_buffer

def get_cached_command(self):
with open("/tmp/vim_python_test_runner_cache", "r") as f:
return f.read()