Skip to content

Instantly share code, notes, and snippets.

@ruandao
Last active July 2, 2019 17:21
Show Gist options
  • Save ruandao/ad016f1b39b1a36b966f01799b83c778 to your computer and use it in GitHub Desktop.
Save ruandao/ad016f1b39b1a36b966f01799b83c778 to your computer and use it in GitHub Desktop.
django auto generate test fileand support doctest
#!/usr/bin/env bash
forModule() {
moduleName=$1
for f in `find ../${moduleName} -name "*.py"`;do
withoutDoubleSlash="${f#*//}"
withoutUpperLevel="${withoutDoubleSlash#../}"
file=$withoutUpperLevel
filename=$(basename $file)
d=$(dirname $file)
templateFile="test_templates/${d}/template"
mkdir -p ${d}
touch ${d}/__init__.py
if [[ $filename == "__init__"* ]];then
continue
fi
# echo "withoutDoubleSlash $withoutDoubleSlash"
# echo "withoutUpperLevel $withoutUpperLevel"
# echo "dirname $d"
# echo "filename $filename"
if [[ -e ${d}/test_${filename} ]];then
echo "file ${d}/test_${filename} exist";
elif [[ -e $templateFile ]];then
cp $templateFile ${d}/test_${filename}
echo "create file ${d}/test_${filename} from template $templateFile"
newVar="${filename%.py}"
echo "<ClassName> --> ${newVar}"
sed -i '' -e "s/<ClassName>/${newVar}/g" ${d}/test_${filename}
fi
done
}
forModule models
# file 'test/templates/models/template'
from django.shortcuts import reverse
from django.contrib.auth import get_user_model
from django.test import TestCase
from learning.models import <ClassName>
# to support doctest
import doctest
doctest.testmod(<ClassName>)
User=get_user_model()
class Test<ClassName>(TestCase):
@classmethod
def setUpTestData(cls):
clsName = str(__class__.__name__)
User.objects.create(username="ruandao" + clsName, email="ljy" + clsName + "@gmail.com")
User.objects.create(username="ruandao123" + clsName, email="ljy123" + clsName + "@gmail.com")
user = User.objects.get(username="ruandao" + clsName, email="ljy" + clsName + "@gmail.com")
user123 = User.objects.get(username="ruandao123" + clsName, email="ljy123" + clsName + "@gmail.com")
pass
def setUp(self) -> None:
pass
def getUser(self):
clsName = str(__class__.__name__)
user = User.objects.get(username="ruandao" + clsName, email="ljy" + clsName + "@gmail.com")
return user
def getUser123(self):
clsName = str(__class__.__name__)
user123 = User.objects.get(username="ruandao123" + clsName, email="ljy123" + clsName + "@gmail.com")
return user123
def fake_request(self, user=None, path='/', method='get', data=None):
from django.test import RequestFactory
from django.contrib.auth.models import AnonymousUser
self.assertTrue(method in ['get', 'post', 'put', 'delete', 'options', 'trace'], f"方法 {method}, 不属于可调用方法('get', 'post', 'put', 'delete', 'head', 'options', 'trace')")
request_factory = RequestFactory()
d = {
'get': request_factory.get,
'post': request_factory.post,
'put': request_factory.put,
'delete': request_factory.delete,
'head': request_factory.head,
'options': request_factory.options,
'trace': request_factory.trace,
}
request = d[method](path, data=data)
request.user = AnonymousUser() if user is None else user
return request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment