diff --git a/cookbook/cgi-apache.zh-cn.md b/cookbook/cgi-apache.zh-cn.md
index 89e51481..12e9d9b9 100644
--- a/cookbook/cgi-apache.zh-cn.md
+++ b/cookbook/cgi-apache.zh-cn.md
@@ -1,22 +1,26 @@
---
layout: default
-title: CGI deployment on Apache
+title: 在 Apache 上部署 CGI
---
-# CGI deployment on Apache
+# 在 Apache 上部署 CGI
-Here are the simple steps needed to create and run an web.py application.
+以下是创建并运行 web.py 应用程序的简单步骤:
-* Install web.py and flups
+* 确保已安装 web.py 和 flup。
+* 创建应用程序
+
+按文档创建应用程序,主代码结构如下:
-* Create the application as documented
if __name__ == "__main__":
web.run(urls, globals())
-For our example, let it be named `app.py`, located in `/www/app` and we need it accessible as `http://server/app/app.py`.
-* Configure Apache (version 2.2 in this example)
+本示例中,我们将应用命名为 `app.py`,存放在 `/www/app` 目录下,并通过 `http://server/app/app.py` 访问。
+
+
+* 配置 Apache(以 Apache 2.2 为例)
ScriptAlias /app "/www/app/"
ps aux | grep code.py
-#Troubleshooting
+# 故障排查
-###Check your apache error log for information!
+
+首要检查 Apache 错误日志为主!
-##Common problems
+## 常见问题
-###File permissions.
-You might see error code 255 in your logs.
-Ensure the directory is readable and that code. py is executable:
+### 文件权限问题
+
+
+日志中可能出现 255 错误码。
+确保目录可读且 code.py 有执行权限:
chmod +x code.py
-###404 Not Found.
-Is your Alias path correct in your apache configuration?
+### 404 Not Found 页面
+
+检查 Apache 配置中的 Alias 路径是否正确?
+
+
+### 其他问题
-###Other problems
Web.py spawns http://0.0.0.0:8080, dies unexpectedly, or returns nothing.
Did you add this line?
-
+
+Web.py 意外退出、无响应或无法启动监听到 http://0.0.0.0:8080 。
+检查是否添加了以下关键代码?
+
+
+
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
-
-#Misc
-After updating your application you may need to restart your web server to see the changes.
+
+
+# 补充说明
+
+更新应用后可能需要重启 Web 服务器才能使修改生效。
+
diff --git a/cookbook/index.zh-cn.md b/cookbook/index.zh-cn.md
index 842c3dbf..77d6375e 100644
--- a/cookbook/index.zh-cn.md
+++ b/cookbook/index.zh-cn.md
@@ -72,8 +72,8 @@ title: Web.py Cookbook 简体中文版
## Testing 测试
-* [Testing with Paste and Nose (未译)](testing_with_paste_and_nose.zh-cn)
-* [RESTful doctesting using an application's request method (未译)](restful_doctesting_using_request.zh-cn)
+* [使用Paste和Nose进行测试](testing_with_paste_and_nose.zh-cn)
+* [使用 app.request 进行 RESTful 文档测试](restful_doctesting_using_request.zh-cn)
## User input 用户输入
@@ -101,11 +101,11 @@ title: Web.py Cookbook 简体中文版
* [通过Fastcgi和lighttpd部署](fastcgi-lighttpd.zh-cn)
* [通过Webpy和Nginx with FastCGI搭建Web.py](fastcgi-nginx.zh-cn)
-* [CGI deployment through Apache (未译)](cgi-apache.zh-cn)
+* [CGI deployment through Apache](cgi-apache.zh-cn)
* mod_python deployment through Apache (requested)
* [通过Apache和mod_wsgi部署](mod_wsgi-apache.zh-cn)
-* [mod_wsgi deployment through Nginx (未译)](mod_wsgi-nginx.zh-cn)
-* [Fastcgi deployment through Nginx (未译)](fastcgi-nginx.zh-cn)
+* [mod_wsgi deployment through Nginx](mod_wsgi-nginx.zh-cn)
+* [Fastcgi deployment through Nginx](fastcgi-nginx.zh-cn)
## Subdomains 子域名
diff --git a/cookbook/restful_doctesting_using_request.zh-cn.md b/cookbook/restful_doctesting_using_request.zh-cn.md
index 14432254..1d6b9fe9 100644
--- a/cookbook/restful_doctesting_using_request.zh-cn.md
+++ b/cookbook/restful_doctesting_using_request.zh-cn.md
@@ -1,9 +1,15 @@
---
layout: default
-title: RESTful doctesting using app.request
+title: 使用 app.request 进行 RESTful 文档测试
---
-# RESTful doctesting using app.request
+# 使用 app.request 进行 RESTful 文档测试
+
+
+`app.request` 是 web.py 框架的测试工具方法,用于在 **不启动服务器** 的情况下模拟 HTTP 请求,主要用于 **单元测试** 或 **文档测试**。
+
+`app.request` 可以模拟请求直接向 web.py 应用发送 HTTP 请求(如 GET/POST)。可以返回包含状态码、响应头和响应内容的 web.Storage 对象。
+
#!/usr/bin/env python
diff --git a/cookbook/testing_with_paste_and_nose.zh-cn.md b/cookbook/testing_with_paste_and_nose.zh-cn.md
index b932a6b4..b555c23e 100644
--- a/cookbook/testing_with_paste_and_nose.zh-cn.md
+++ b/cookbook/testing_with_paste_and_nose.zh-cn.md
@@ -1,15 +1,15 @@
---
layout: default
-title: Testing with Paste and Nose
+title: 使用Paste和Nose进行测试
---
-# Testing with Paste and Nose
+# 使用Paste和Nose进行测试
-## Problem
+## 问题描述
-You want to test your web.py application.
+你需要对 web.py 应用进行自动化测试。
-## Solution
+## 解决方案
from paste.fixture import TestApp
from nose.tools import *
@@ -23,36 +23,50 @@ You want to test your web.py application.
assert_equal(r.status, 200)
r.mustcontain('Hello, world!')
-## Background
+## 技术背景
-This example makes use of the Paste and Nose libraries. [Paste](http://pythonpaste.org/) lets you throw test requests at your application, and adds some helpful [custom methods to the response objects](http://pythonpaste.org/webtest/#the-response-object), such as mustcontain(), seen above. [Nose](http://somethingaboutorange.com/mrl/projects/nose/) makes writing and running your tests dead simple. When run from the base of your tree, it automatically finds and runs anything which is named like a test, adding necessary modules to your PYTHONPATH. This gives you the flexibility to run your tests from other directories, as well. Another benefit of Nose is that you no longer need to have every test class inherit from unittest.TestCase. Many more details are outlined on the project page.
+本方案使用两个关键库`Paste`和`Nose`协助你完成测试:
-## Explanation
+[Paste](http://pythonpaste.org/) :提供模拟请求功能,并为响应对象添加[实用方法](http://pythonpaste.org/webtest/#the-response-object)方便你去使用。(如示例中的 `mustcontain()`)
+
+[Nose](http://somethingaboutorange.com/mrl/projects/nose/) :可以帮助你简化测试编写和运行代码,自动发现并执行测试文件,支持灵活目录结构运行
+当你在你的项目目录下执行测试时,该工具可以自动识别并且运行所有符合测试命名规范的文件(例如 `test_*.py`),同时也会动态的将必要模块添加到 `PYTHONPATH`。即使从其他子目录运行测试命令(如 subdir/nosetests),Nose 仍能正确解析路径并执行测试。另一个好处是,测试类无需强制继承 `unittest.TestCase`,减少冗余代码,提升代码可读性。
+
+
+## 代码解析
+
+
+该测试代码位于 test_code.py 文件中,项目目录结构如下:
-This code resides in a file called test_code.py. The directory layout of the application looks like this:
./
code.py
./test
test_code.py
-Most of the code example above should be fairly self-explanatory. From our main module, code, we import app, which is defined in the usual way:
+大部分代码示例应该是容易理解的。从主模块 code 导入已定义的 web.py 应用对象:
app = web.application(urls, globals())
-To set up the test, we pass its wsgifunc() to Paste's TestApp, as you have already seen in the example.
+在设置测试用例的初始化环节,我们将 `wsgifunc()` 传递给 Paste 的 TestApp,如下面代码所示:
app = TestApp(app.wsgifunc(*middleware))
-assert_equal() is one of the methods provided by nose's utils, and works just like unittest's assertEqual().
-## Setting Up the Test Environment
+`assert_equal()` 断言是 Nose 提供的工具方法,功能等同于 unittest 的 assertEqual():
-In order to avoid kicking off web.py's webserver when we run our tests, a change is required to the line which calls run(). It normally looks something like this:
+ assert_equal(response.status, 200) # 验证状态码是否为 200
- if __name__ == "__main__": app.run()
+## 测试环境配置
+
+为避免运行测试时自动启动 web.py 的 Web 服务器,需修改调用 app.run() 的代码。原始代码通常如下:
+
+ if __name__ == "__main__":
+ app.run()
+
+我们可以通过读取环境变量的方式去区分测试还是实际部署运行,运行测试时,设置环境变量 `WEBPY_ENV=test` 标识测试环境。
+然后,在程序启动阶段,添加环境检测函数 `is_test()`,整体代码将变成如下形式:
-We can define an environment variable, such as WEBPY_ENV=test, when we run our tests. In that case, the above line becomes the following:
import os
@@ -60,10 +74,15 @@ We can define an environment variable, such as WEBPY_ENV=test, when we run our t
if 'WEBPY_ENV' in os.environ:
return os.environ['WEBPY_ENV'] == 'test'
- if (not is_test()) and __name__ == "__main__": app.run()
+ if (not is_test()) and __name__ == "__main__":
+ app.run()
-Then, it's simply a matter of running nosetests like so:
+如此一来,启动测试时附加环境变量即可:
WEBPY_ENV=test nosetests
-The is_test() function comes in handy for other things, such as doing conditional database commits to avoid test database pollution.
+
+`is_test()` 函数还可用于其他场景,例如,测试环境下跳过数据库提交,避免污染测试数据:
+
+ if not is_test():
+ db.commit()