测试自动化 2011/06/11 -2

来源:互联网 发布:天龙八部暗器炼毒数据 编辑:程序博客网 时间:2024/07/24 02:37

0. 杂记与杂问

 

Q:如果要选择性执行一些test cases,如何配置???

* Const的一些用法总结:

http://topic.csdn.net/t/20030421/20/1690292.html

 

# 指针相关

const   int*   a   =   &b;   [1]
int   const   *a   =   &b; [2]
int*   const   a   =   &b; [3]
const   int*   const   a   =   &b; [4]

 

# 函数相关

一般放在函数体后,形如:void   fun()   const;
如果一个成员函数的不会修改数据成员,那么最好将其声明为const,因为const成员函数中不允许对数据成员进行修改,如果修改,编译器将报错,这大大提高了程序的健壮性

 

#几点关于const的建议

1   要大胆的使用const,这将给你带来无尽的益处,但前提是你必须搞清楚原委;
2   要避免最一般的赋值操作错误,如将const变量赋值,具体可见思考题;
3   在参数中使用const应该使用引用或指针,而不是一般的对象实例,原因同上;
4   const在成员函数中的三种用法(参数、返回值、函数)要很好的使用;
5   不要轻易的将函数的返回值类型定为const;
6除了重载操作符外一般不要将返回值类型定为对某个对象的const引用

 

 

 

1. GMock: Nice Mocks and Strict Mocks

 

* 增加对unintersting方法Do()的调用

在class Hello中增加方法Do().

 

[root@localhost HelloGMock]# make
g++ -I../../gtest/include -I../../include -g -Wall -Wextra -c ./SayHelloTest.cpp ./Hello.h ./MockHello.h ./SayHello.h
./Hello.h:4: 警告:‘class Hello’ 有虚函数却没有虚析构函数
./MockHello.h:6: 警告:‘class MockHello’ 有虚函数却没有虚析构函数
./SayHelloTest.cpp: In member function ‘virtual void SayHelloTest_test_Test::TestBody()’:
./SayHelloTest.cpp:8: 错误:不能将变量 ‘hello’ 声明为具有抽象类型 ‘MockHello’
./MockHello.h:6: 附注: 因为下列虚函数在 ‘MockHello’ 中为纯虚函数:
./Hello.h:7: 附注:     virtual void Hello::Do()

 

解决办法:在MockHello.h中增加对Do()的Mock方法

 

[root@localhost HelloGMock]# cat MockHello.h

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "Hello.h"

class MockHello : public Hello {
public:
        MOCK_METHOD0(Hi, void());
        MOCK_METHOD0(Do, void());
};

注:所有方法必须要MOCK

 

* 使用NiceMock<>

 

./SayHelloTest.cpp: In member function ‘virtual void SayHelloTest_test_Test::TestBody()’:
./SayHelloTest.cpp:9: 错误:‘NiceMock’ 在此作用域中尚未声明
./SayHelloTest.cpp:9: 错误:expected primary-expression before ‘>’ token
./SayHelloTest.cpp:9: 错误:‘hello’ 在此作用域中尚未声明

 

使用using ::testing::NiceMock

OK !

 

* 使用StrictMock<>

 

[root@localhost HelloGMock]# ./HelloGMock
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from SayHelloTest
[ RUN      ] SayHelloTest.test
unknown file: Failure
Uninteresting mock function call - returning directly.
    Function call: Do()
[  FAILED  ] SayHelloTest.test (6 ms)
[----------] 1 test from SayHelloTest (9 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (19 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] SayHelloTest.test

 1 FAILED TEST

 

2. 重温第一个GTest例子:FooTest

 

* make file


TEST_SRC = $(USER_DIR)/FooTest.cpp $(USER_DIR)/Foo.cpp $(USER_DIR)/FooEnvironment.cpp
xxx : $(TEST_SRC) $(GTEST_HEADERS)
#       $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/FooTest.cpp

FooTest : Foo.o  FooTest.o FooEnvironment.o
#       $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread -lgtest -L../lib/.libs $^ -o $@

#FooTest : xxx
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread -lgtest -L../lib/.libs $^ -o $@
~
~

注:

xxx : $(TEST_SRC) $(GTEST_HEADERS)

会自动解释成以下语句:


g++ -g -Wall -Wextra -I../include  -c -o Foo.o Foo.cpp
g++ -g -Wall -Wextra -I../include  -c -o FooTest.o FooTest.cpp
g++ -g -Wall -Wextra -I../include  -c -o FooEnvironment.o FooEnvironment.cpp

 

3. 读sample1_unittest.cc源代码

 

* 头文件

#include "gtest/gtest.h"  // 唯一外部头文件

 

* TEST

TEST(test-case-name, test-name)

 

// Google Test guarantees that each test you define is run exactly
// once, but it makes no guarantee on the order the tests are
// executed.  Therefore, you should write your tests in such a way
// that their results don't depend on their order.

例子:

 

TEST(FactorialTest, Negative) {
  // This test is named "Negative", and belongs to the "FactorialTest"
  // test case.
  EXPECT_EQ(1, Factorial(-5));
  EXPECT_EQ(1, Factorial(-1));
  EXPECT_TRUE(Factorial(-10) > 0);
}

注:最终展开成FactgorialTest.Negative的形式,关注”当负数“时函数的反应。

 

* EXPECT_EQ(expected, actual)

* EXPECT_TRUE(true/false)

* gtest_main.cc

#include <iostream>

#include "gtest/gtest.h"

GTEST_API_ int main(int argc, char **argv) {
  std::cout << "Running main() from gtest_main.cc/n";

  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

 

Q:如果要选择性执行一些test cases,如何配置???