@@ -1184,6 +1184,12 @@ C | 2
1184
1184
1185
1185
# #Google Ngx Pagespeed
1186
1186
1187
+ CLA: Contributor License Agreement
1188
+
1189
+ ! [Google CLA](./img/google-cla.png)
1190
+
1191
+ ! [Eclipse CLA](./img/eclipse-cla.png)
1192
+
1187
1193
` ` `
1188
1194
else
1189
1195
cat << END
@@ -1665,6 +1671,163 @@ req.end();
1665
1671
1666
1672
在这种理想的情况下,我们为什么不TDD呢?
1667
1673
1674
+
1675
+ # #轻量级网站测试TWill
1676
+
1677
+ > twill was initially designed for testing Web sites, although since then people have also figured out that it' s good for browsing unsuspecting Web sites.
1678
+
1679
+ 之所以说轻量的原因是他是拿命令行测试的,还有DSL,还有Python。
1680
+
1681
+ 除此之外,还可以拿它做压力测试,这种压力测试和一般的不一样。可以模拟整个过程,比如同时有多少人登陆你的网站。
1682
+
1683
+ 不过,它有一个限制是没有JavaScript。
1684
+
1685
+ 看了一下源码,大概原理就是用``requests``下载html,接着用``lxml``解析html,比较有意思的是内嵌了一个``DSL``。
1686
+
1687
+ 这是一个Python的库。
1688
+
1689
+ pip install twill
1690
+
1691
+ ##Twill 登陆测试
1692
+
1693
+ 1.启动我们的应用。
1694
+
1695
+ 2.进入twill shell
1696
+
1697
+ twill-sh
1698
+ -= Welcome to twill! =-
1699
+ current page: *empty page*
1700
+
1701
+ 3.打开网页
1702
+
1703
+ >> go http://127.0.0.1:5000/login
1704
+ ==> at http://127.0.0.1:5000/login
1705
+ current page: http://127.0.0.1:5000/login
1706
+
1707
+ 4.显示表单
1708
+
1709
+ >> showforms
1710
+
1711
+ Form #1
1712
+ ## ## __Name__________________ __Type___ __ID________ __Value__________________
1713
+ 1 csrf_token hidden csrf_token 1423387196##5005bdf3496e09b8e2fbf450 ...
1714
+ 2 email email email None
1715
+ 3 password password password None
1716
+ 4 login submit (None) 登入
1717
+
1718
+ current page: http://127.0.0.1:5000/login
1719
+
1720
+ 5.填充表单
1721
+
1722
+ formclear 1
1723
+ fv 1 email test@tes.com
1724
+ fv 1 password test
1725
+
1726
+ 6.修改action
1727
+
1728
+ formaction 1 http://127.0.0.1:5000/login
1729
+
1730
+ 7.提交表单
1731
+
1732
+ >> submit
1733
+ Note: submit is using submit button: name="login", value="登入"
1734
+ current page: http://127.0.0.1:5000/
1735
+
1736
+ 发现重定向到首页了。
1737
+
1738
+ ##Twill 测试脚本
1739
+
1740
+ 当然我们也可以用脚本直接来测试``login.twill``:
1741
+
1742
+ go http://127.0.0.1:5000/login
1743
+
1744
+ showforms
1745
+ formclear 1
1746
+ fv 1 email test@tes.com
1747
+ fv 1 password test
1748
+ formaction 1 http://127.0.0.1:5000/login
1749
+ submit
1750
+
1751
+ go http://127.0.0.1:5000/logout
1752
+
1753
+ 运行
1754
+
1755
+ twill-sh login.twill
1756
+
1757
+ 结果
1758
+
1759
+ >> EXECUTING FILE login.twill
1760
+ AT LINE: login.twill:0
1761
+ ==> at http://127.0.0.1:5000/login
1762
+ AT LINE: login.twill:2
1763
+
1764
+ Form #1
1765
+ ## ## __Name__________________ __Type___ __ID________ __Value__________________
1766
+ 1 csrf_token hidden csrf_token 1423387345##7a000b612fef39aceab5ca54 ...
1767
+ 2 email email email None
1768
+ 3 password password password None
1769
+ 4 login submit (None) 登入
1770
+
1771
+ AT LINE: login.twill:3
1772
+ AT LINE: login.twill:4
1773
+ AT LINE: login.twill:5
1774
+ AT LINE: login.twill:6
1775
+ Setting action for form (<Element form at 0x10e7cbb50>,) to (' http://127.0.0.1:5000/login' ,)
1776
+ AT LINE: login.twill:7
1777
+ Note: submit is using submit button: name="login", value="登入"
1778
+
1779
+ AT LINE: login.twill:9
1780
+ ==> at http://127.0.0.1:5000/login
1781
+ --
1782
+ 1 of 1 files SUCCEEDED.
1783
+
1784
+ 一个成功的测试诞生了。
1785
+
1786
+ ##Fake Server
1787
+
1788
+ 实践了一下怎么用sinon去fake server,还没用respondWith,于是写一下。
1789
+
1790
+ 这里需要用到sinon框架来测试。
1791
+
1792
+ 当我们fetch的时候,我们就可以返回我们想要fake的结果。
1793
+
1794
+ var data = {"id":1,"name":"Rice","type":"Good","price":12,"quantity":1,"description":"Made in China"};
1795
+ beforeEach(function() {
1796
+ this.server = sinon.fakeServer.create();
1797
+ this.rices = new Rices();
1798
+ this.server.respondWith(
1799
+ "GET",
1800
+ "http://localhost:8080/all/rice",
1801
+ [
1802
+ 200,
1803
+ {"Content-Type": "application/json"},
1804
+ JSON.stringify(data)
1805
+ ]
1806
+ );
1807
+ });
1808
+
1809
+ 于是在afterEach的时候,我们需要恢复这个server。
1810
+
1811
+ afterEach(function() {
1812
+ this.server.restore();
1813
+ });
1814
+
1815
+ 接着写一个jasmine测试来测试
1816
+
1817
+ describe("Collection Test", function() {
1818
+ it("should get data from the url", function() {
1819
+ this.rices.fetch();
1820
+ this.server.respond();
1821
+ var result = JSON.parse(JSON.stringify(this.rices.models[0]));
1822
+ expect(result["id"])
1823
+ .toEqual(1);
1824
+ expect(result["price"])
1825
+ .toEqual(12);
1826
+ expect(result)
1827
+ .toEqual(data);
1828
+ });
1829
+ });
1830
+
1668
1831
#重构
1669
1832
1670
1833
或许你应该知道了,重构是怎样的,你也知道重构能带来什么。在我刚开始学重构和设计模式的时候,我需要去找一些好的示例,以便于我更好的学习。有时候不得不创造一些更好的场景,来实现这些功能。
0 commit comments