博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Karma自动化测试
阅读量:2150 次
发布时间:2019-04-30

本文共 6551 字,大约阅读时间需要 21 分钟。

Karma自动化测试

什么时候启用自动化测试?

项目开发到后期,版本相对稳定,需要进行反复测试的情况下可以考虑启用自动化测试,但不能完全替代手工测试,自动化测试仅是减去测试人员反复测试的工作量.

Angularjs测试环境搭建

  1. 安装node.js
  2. 安装karma(在软件根目录安装)
    npm install -g karma
    npm install karma-qunit --save-dev
    npm install karma-chrome-launcher --save-dev
  3. karma init配置karma.config.js

Controller测试用例

JS:.controller("defaultCtrl", function ($scope, $http, $interval, $timeout, $log) {
$scope.intervalCounter = 0; $scope.timerCounter = 0; $interval(function () {
$scope.intervalCounter++; }, 5, 10); $timeout(function () {
$scope.timerCounter++; }, 5); $scope.products=[]; $http.get("productData.json").success(function (data) {
$scope.products = data; $log.log("There are " + data.length + " items"); }); $scope.counter = 0; $scope.incrementCounter = function () {
$scope.counter++; } })Controller Test:describe("Controller Test", function () {
// Arrange var mockScope, controller, backend, mockInterval, mockTimeout, mockLog; //angular.mock用于创建模拟模块和解决依赖关系。 beforeEach(angular.mock.module("exampleApp")); //测试前执行的函数 beforeEach(angular.mock.inject(function ($httpBackend) {
//inject解决依赖关系注入到一个函数。 backend = $httpBackend; backend.expect("GET", "productData.json").respond(//模拟Ajax [{ "name": "Apples", "category": "Fruit", "price": 1.20 }, { "name": "Bananas", "category": "Fruit", "price": 2.42 }, { "name": "Pears", "category": "Fruit", "price": 2.02 }]); })); beforeEach(angular.mock.inject(function ($controller, $rootScope, $http, $interval, $timeout, $log) {
//依赖注入 mockScope = $rootScope.$new();//Creates a new scope mockInterval = $interval; mockTimeout = $timeout; mockLog = $log; //$controller(name) Creates an instance of the specified controller $controller("defaultCtrl", { $scope: mockScope, $http: $http, $interval: mockInterval, $timeout: mockTimeout, $log: mockLog }); backend.flush();//解决$http承诺 })); // Act and Assess it("Creates variable", function () {
expect(mockScope.counter).toEqual(0); }) it("Increments counter", function () {
mockScope.incrementCounter(); expect(mockScope.counter).toEqual(1);//判断值mockScope.counter==1? }); it("Makes an Ajax request", function () {
backend.verifyNoOutstandingExpectation(); }); it("Processes the data", function () {
expect(mockScope.products).toBeDefined(); expect(mockScope.products.length).toEqual(3); }); it("Preserves the data order", function () {
expect(mockScope.products[0].name).toEqual("Apples"); expect(mockScope.products[1].name).toEqual("Bananas"); expect(mockScope.products[2].name).toEqual("Pears"); }); it("Limits interval to 10 updates", function () {
for (var i = 0; i < 11; i++) { mockInterval.flush(5000); } expect(mockScope.intervalCounter).toEqual(10); }); it("Increments timer counter", function () {
mockTimeout.flush(5000); expect(mockScope.timerCounter).toEqual(1); }); it("Writes log messages", function () {
expect(mockLog.log.logs.length).toEqual(1); });});

Filter测试用例

JS:.filter("labelCase", function () {
return function (value, reverse) {
if (angular.isString(value)) { var intermediate = reverse ? value.toUpperCase() : value.toLowerCase(); return (reverse ? intermediate[0].toLowerCase() : intermediate[0].toUpperCase()) + intermediate.substr(1); } else { return value; } }; })Filter Tests:describe("Filter Tests", function () {
var filterInstance; beforeEach(angular.mock.module("exampleApp")); beforeEach(angular.mock.inject(function ($filter) {
filterInstance = $filter("labelCase"); })); it("Changes case", function () {
var result = filterInstance("test phrase"); expect(result).toEqual("Test phrase"); }); it("Reverse case", function () {
var result = filterInstance("test phrase", true); expect(result).toEqual("tEST PHRASE"); });});

Directive测试用例

js:.directive("unorderedList", function () {
return function (scope, element, attrs) {
var data = scope[attrs["unorderedList"]]; if (angular.isArray(data)) { var listElem = angular.element("
    "); element.append(listElem); for (var i = 0; i < data.length; i++) { listElem.append(angular.element('
  • ').text(data[i].name)); } } } })Directive Tests:describe("Directive Tests", function () {
    var mockScope; var compileService; beforeEach(angular.mock.module("exampleApp")); beforeEach(angular.mock.inject(function ($rootScope, $compile) {
    mockScope = $rootScope.$new(); compileService = $compile; mockScope.data = [ { name: "Apples", category: "Fruit", price: 1.20, expiry: 10 }, { name: "Bananas", category: "Fruit", price: 2.42, expiry: 7 }, { name: "Pears", category: "Fruit", price: 2.02, expiry: 6 }]; })); it("Generates list elements", function () {
    var compileFn = compileService("
    "); var elem = compileFn(mockScope); expect(elem.children("ul").length).toEqual(1); expect(elem.find("li").length).toEqual(3); expect(elem.find("li").eq(0).text()).toEqual("Apples"); expect(elem.find("li").eq(1).text()).toEqual("Bananas"); expect(elem.find("li").eq(2).text()).toEqual("Pears"); });});

Service测试用例

js:.factory("counterService", function () {
var counter = 0; return { incrementCounter: function () {
counter++; }, getCounter: function () {
return counter; } } });Service Testsdescribe("Service Tests", function () {
beforeEach(angular.mock.module("exampleApp")); it("Increments the counter", function () {
angular.mock.inject(function (counterService) {
expect(counterService.getCounter()).toEqual(0); counterService.incrementCounter(); expect(counterService.getCounter()).toEqual(1); }); });});

转载地址:http://cddwb.baihongyu.com/

你可能感兴趣的文章
Eclipse Memory Analyzer 使用技巧
查看>>
tomcat连接超时
查看>>
谈谈编程思想
查看>>
iOS MapKit导航及地理转码辅助类
查看>>
检测iOS的网络可用性并打开网络设置
查看>>
简单封装FMDB操作sqlite的模板
查看>>
iOS开发中Instruments的用法
查看>>
iOS常用宏定义
查看>>
什么是ActiveRecord
查看>>
有道词典for mac在Mac OS X 10.9不能取词
查看>>
关于“团队建设”的反思
查看>>
利用jekyll在github中搭建博客
查看>>
Windows7中IIS简单安装与配置(详细图解)
查看>>
linux基本命令
查看>>
BlockQueue 生产消费 不需要判断阻塞唤醒条件
查看>>
强引用 软引用 弱引用 虚引用
查看>>
数据类型 java转换
查看>>
"NetworkError: 400 Bad Request - http://172.16.47.117:8088/rhip/**/####t/approval?date=976
查看>>
mybatis 根据 数据库表 自动生成 实体
查看>>
win10将IE11兼容ie10
查看>>