我的项目(Django)有任何更改时如何自动运行测试?
目前,在对django项目进行重大更改后,我偶尔会运行python manage.py test
。 每当我更改并将文件保存在项目中时,是否可以自动运行那些测试? 尽早发现错误将很有用(我知道Rails在rspec中有类似的东西)。 我正在用鼻子和Django-nose。 提前致谢。
使用entr:
$ find . -name '*.py' | entr python ./manage.py test
或者,为了获得额外的信誉,请将其与ack结合使用:
$ ack --python | entr python ./manage.py test
如果您希望它甚至在添加新文件时都能找到它们:
$ until ack -f --python | entr -d python ./manage.py test; do sleep 1; done
py.test答案(也适用于鼻子):
pip install pytest-xdist
py.test -f # will watch all subfolders for changes, and rerun the tests
由于py.test可以识别鼻子,因此它也适用于鼻子。
我是一名JavaScript开发人员,因此我使用JS开发人员使用Node.js构建的工具在我的项目中实现了相同的目标。 这非常简单,但是您还需要安装nodeJS才能使其正常运行。
我在项目根目录中创建了一个名为gruntfile.js的文件:
//gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
watch: {
files: ['*.py'],
tasks: ['shell']
},
shell: {
test: {
command: 'python main_test.py'
}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-shell');
grunt.registerTask('default', ['watch']);
};
它的作用是基本上监视该目录中具有py扩展名的任何文件,如果更改了该文件,则执行一个shell命令,在这种情况下,这是我的python测试(您可能想要更改它,我的测试名称是main_test.py)。 为了运行此grunt脚本,您需要安装Node.js,然后在全局路径中使用npm。 之后,您还需要安装一些节点模块。 除grunt-cli之外的所有这些模块都将存储在当前文件夹中,因此请确保您位于项目的根目录或将gruntfile.js放入的文件夹中,然后运行替代命令。
npm install grunt-cli -g
npm install grunt
npm install grunt-contrib-watch
npm install grunt-shell
不必担心大小,它们是非常小的模块。 既然您已完成所有设置,则只需运行3005360922123830830272,它将开始监视py文件,保存后将运行测试。 这可能不是运行python测试的最佳方法,但是正如我所说的,我是一名JavaScript开发人员,我认为Grunt提供了一种非常简单的执行测试的方法,即使对于其他语言也是如此,因此我使用它。
我刚试过--ignore-dirs
,效果很好! 安装插件并使用--ignore-dirs
选项运行测试。
更新::(当从django-nose的manage.py帮助程序中运行测试时,它似乎无法正常工作。
最终,我选择使用支持django的tdaemon,尽管对于成熟的项目可能也需要一些摆弄。
例如,这是我为django项目运行的方式:
tdaemon -t django --custom-args=a_specific_app_to_test -d --ignore-dirs logs
--ignore-dirs
会将测试重点放在特定的应用程序上(与tdaemon
相同)
--ignore-dirs
参数用于启用调试日志记录,该日志记录打印触发了运行的文件更改。
--ignore-dirs
是必需的,因为我的测试写入了日志(这本身就是一个问题!),并且tdaemon
陷入了无限循环。
这里的另一个Javascript开发人员,我发现*.py
([https://github.com/remy/nodemon)]可以很好地工作。 默认情况下,它监视*.js
文件,但是可以使用--ext
标志进行配置。 要使用它,请执行以下操作:
npm install -g nodemon
cd /your/project/dir/
nodemon --ext py --exec "python manage.py test"
现在,只要*.py
文件发生更改,它将重新运行您的命令。 它甚至可以找到新文件。
我用过手表,像手表
我用口水做到了这一点。 安装gulp-shell:
npm install gulp-shell --save-dev
并在gulpfile.js中:
var shell = require('gulp-shell')
gulp.task('run-tests', shell.task([
'python3 manage.py test']))
gulp.task('watch', function(){
gulp.watch(['./your-project-name/**/*.html', './your-project-name/**/*.py'], ['run-tests']);
});
gulp.task('default',['run-tests','watch']);
并且只要有更改保存到任何.py或.html文件,它就可以运行测试!
您可以在Django之上使用Django Supervisor。 这将避免使用CI工具(在任何情况下都可能有用,这不会使其他响应无效-可能只是互补的)。
我建议设置django-nose和嗅探器。 设置非常容易,效果很好。 我唯一需要的就是与scent.py类似的东西。 然后,您可以运行sniffer -x myapp.tests
。
鼻子还带有其他一些东西,这些东西也使测试变得更好用。
如果您使用git控制代码,另一种方法是使用git hook pre-commit
可能出现类似remote: fatal: Not a git repository: '.'
的错误,请查看此帖子[https://stackoverflow.com/a/4100577/7007942]
我编写了一个Gulp任务,可以在任何指定的Python文件被更改或删除时自动运行node node_modules/gulp/bin/gulp.js watch
。 为此,您将需要Node。
首先,安装Gulp:
yarn add -D gulp@next
然后使用以下node node_modules/gulp/bin/gulp.js watch
进行必要的调整:
const { exec } = require('child_process');
const gulp = require('gulp');
const runDjangoTests = (done) => {
const task = exec('./manage.py test --keepdb', {
shell: '/bin/bash', // Accept SIGTERM signal. Doesn't work with /bin/sh
});
task.stdout.pipe(process.stdout);
task.stderr.pipe(process.stderr);
task.on('exit', () => {
done();
});
return task;
};
gulp.task('test', runDjangoTests);
gulp.task('watch', (done) => {
let activeTask;
const watcher = gulp.watch('**/*.py', {
// Ignore whatever directories you need to
ignored: ['.venv/*', 'node_modules'],
});
const runTask = (message) => {
if (activeTask) {
activeTask.kill();
console.log('\n');
}
console.log(message);
activeTask = runDjangoTests(done);
};
watcher.on('change', (path) => {
runTask(`File ${path} was changed. Running tests:`);
});
watcher.on('unlink', (path) => {
runTask(`File ${path} was removed. Running tests:`);
});
});
然后只需运行node node_modules/gulp/bin/gulp.js watch
以运行任务:)
[HTTPS://培养出润肠.com/]
关键功能之一-仅对文件更改运行受影响的测试。 可作为PyCharm的插件
您将需要一个持续集成服务器,例如Jenkins。