如何在恢复提交和保留单元测试的同时检查测试?

时间:2016-02-19 14:30:46

标签: c++ git unit-testing software-design

我正在使用一个开源项目。实际上我有一个主人的克隆,我正在编写单元测试。现在写完单元测试后说在abc.cxx中 我想通过恢复提交来检查我的测试是否失败(我有实际修复错误的补丁的提交ID)。

那么通过git命令检查这个的最佳方式是什么。我已经尝试过git revert,但我的测试也被删除了(在abc.cxx中),我无法测试该功能。

我是git的初学者并导致任何类型的错误都可能导致我重建主人(6-7小时)。

1 个答案:

答案 0 :(得分:2)

我建议:

  1. 在专门的临时分支中提交您的单元测试,
  2. 恢复此分支中的错误修复,
  3. 测试您的单元测试,
  4. 如果一切正常,请将您的单元测试重新合并/合并到主
  5. 演示:

    # INIT
    $ git init test
    Initialized empty Git repository in /tmp/test/.git/
    $ cd test/
    $ echo bug > file
    $ git add file
    $ git commit -m"Commit with bugs in it"
    [master (root-commit) 498680f] Commit with bugs in it
     1 files changed, 1 insertions(+), 0 deletions(-)
     create mode 100644 file
    $ echo feature > file
    $ git commit -am"Fix bug #1"
    [master 05540d3] Fix bug #1
     1 files changed, 1 insertions(+), 1 deletions(-)
    
    # (1)
    $ echo 42 > unit-test
    $ git add unit-test
    $ git checkout -b unit-test
    A   unit-test
    Switched to a new branch 'unit-test'
    $ git commit -m"Add unit test for bug #1"
    [unit-test 240bc6c] Add unit test for bug #1
     1 files changed, 1 insertions(+), 0 deletions(-)
     create mode 100644 unit-test
    $ git l
    * 240bc6c (HEAD, unit-test) Add unit test for bug #1
    * 05540d3 (master) Fix bug #1
    * 498680f Commit with bugs in it
    
    # (2)
    $ git revert 05540d3
    Finished one revert.
    [unit-test 5f5eb62] Revert "Fix bug #1"
     1 files changed, 1 insertions(+), 1 deletions(-)
    $ git l
    * 5f5eb62 (HEAD, unit-test) Revert "Fix bug #1"
    * 240bc6c Add unit test for bug #1
    * 05540d3 (master) Fix bug #1
    * 498680f Commit with bugs in it
    
    # (3)
    $ # check for bug
    
    # (4)
    $ git checkout master
    Switched to branch 'master'
    $ git rebase 240bc6c
    First, rewinding head to replay your work on top of it...
    Fast-forwarded master to 240bc6c.
    $ git branch -D unit-test
    Deleted branch unit-test (was 5f5eb62).
    $ git l
    * 240bc6c (HEAD, master) Add unit test for bug #1
    * 05540d3 Fix bug #1
    * 498680f Commit with bugs in it