重構 if X is None¶
Reproducer¶
$ pip install nose
$ pip install https://github.com/sixty-north/cosmic-ray/zipball/master
$ cosmic-ray run --test-runner nose --baseline=10 example.json hello.py -- test_hello.py
$ cosmic-ray report example.json
job ID 4:Outcome.SURVIVED:hello
command: cosmic-ray worker hello replace_Is_with_Eq 0 nose -- -v test_hello.py
--- mutation diff ---
--- a/example_06/hello.py
+++ b/example_06/hello.py
@@ -1,7 +1,7 @@
def sayHello(name, title=None):
- if (title is None):
+ if (title == None):
title = 'Mr.'
return ('Hello %s %s' % (title, name))
total jobs: 9
complete: 9 (100.00%)
survival rate: 11.11%
備註
PR #162 Cosmic-Ray 跳過了像是 == -> is
的突變,但是沒有跳過相反的``is -> ==`` 部份。
現在比較重構後的結果:
$ cosmic-ray run --test-runner nose --baseline=10 example.json hello2.py -- test_hello2.py
$ cosmic-ray report example.json --full-report
total jobs: 0
no jobs completed
行為上完全相等但是我們降低了突變可能的數量。
程式碼¶
def sayHello(name, title=None):
if title is None:
title = "Mr."
return "Hello %s %s" % (title, name)
import hello
import unittest
class TestHello(unittest.TestCase):
def test_sayHello_without_title(self):
result = hello.sayHello("Senko")
self.assertEqual(result, "Hello Mr. Senko")
def test_sayHello_with_title(self):
result = hello.sayHello("Senko", title="The Misterious")
self.assertEqual(result, "Hello The Misterious Senko")
if __name__ == "__main__":
unittest.main()
def sayHello(name, title=None):
if not title:
title = "Mr."
return "Hello %s %s" % (title, name)
import hello
import unittest
class TestHello(unittest.TestCase):
def test_sayHello_without_title(self):
result = hello.sayHello("Senko")
self.assertEqual(result, "Hello Mr. Senko")
def test_sayHello_with_title(self):
result = hello.sayHello("Senko", title="The Misterious")
self.assertEqual(result, "Hello The Misterious Senko")
if __name__ == "__main__":
unittest.main()