Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm - run a corresponding unit test each time a file is saved

I have just switched over to using PyCharm as my primary Python editor, and am curious to know how I can configure it so that every time I save a file, that the unit-test corresponding to the modified file will be run? (after poking around the documentation, I have not found a clear explanation of how to do this)

like image 281
Alexander Marquardt Avatar asked Sep 22 '14 18:09

Alexander Marquardt


3 Answers

When you run a unittest you get a new unittest window. In this window there's a button where you can toggle auto-testing on and off, see below. You can set the auto-test delay as well (look for the gear-icon in the same window.

auto.test

like image 60
Olav Avatar answered Nov 14 '22 21:11

Olav


You can try File Watchers plugin to execute task on files modification.

example config: Config

like image 45
Kamil Avatar answered Nov 14 '22 22:11

Kamil


Here is the way that I decided to accomplish my objective.

I use file watchers (PyCharm->Preferences->File Watchers) to monitor the python source files for modifications. Source files are located in a directory called src. For my purposes, each unit test is located in a directory that is parallel to the src directory called test_src, in which each file src/[file_name].py will have an associated test file called test_src/test_[file_name].py. Unit tests will by run by py.test.

To accomplish this, I use the following setup in the File Watchers dialog:

File type: "Python Files"
Scope: "Only watch files in src directory"
Program: /usr/local/bin/py.test
Arguments: $ProjectFileDir$/test_src/test_$FileName$
Working directory: $ProjectFileDir$

I also unchecked "Immediate file synchronization" as I only want to run the unit tests when the file is saved.

Note that the "Scope" is used for specifying which files will be monitored.

like image 22
Alexander Marquardt Avatar answered Nov 14 '22 21:11

Alexander Marquardt