簡體   English   中英

如何在python unittest中為此類使用setUp和tearDown

[英]How to use setUp and tearDown in python unittest for this class

我很難學習測試驅動開發。

我正在編寫一個類,它將采用文件名或文件描述並調整輸入大小,並從文件中返回大小的數據塊。

在開始測試時,我首先要測試傳遞的參數是否為none,並檢查參數是否是有效的文件對象。

所有我可以集中在代碼之下,我是否應該使用setUp和tearDown方法,或者它是完全錯誤的? 我正在創建一個臨時文件和類的實例,定義為在setUp()中讀取,我應該在tearDown()中刪除對象

下面是代碼

class Test_FileChunk(unittest.TestCase):
    """
    """
    def setUp(self):
        self.fhandle, self.fname = mkstemp()
        self.fc_obj = FileChunk(filename=self.fname)

    def tearDown(self):
        try:
            os.remove(self.fname)
        except OSError as oserr:
            print(oserr)

    def test_instance_variables(self):
        self.assertIsNotNone(self.fc_obj.filename)
        self.assertIsNone(self.fc_obj.filehandle)
        self.assertEqual(self.fc_obj.chunk_size, 8192)

    def test_check_if_instance_variables_are_valid_file_objects(self):
        handle = open(self.fc_obj.filename
        self.assertEqual(
            hasattr
                (handle, "r"), 'seek'), True,
                    msg="Is not a valid file object")
        handle.close()

我在stackoverflow上進行了多個TDD問題並提出了教程,但是看起來跟TDD教程看起來很有意思,但實際上做TDD非常困難。 我實際上可以想到我想在ReadChunk類中做什么,但實際上我無法先了解實際找到測試然后編寫代碼。 我能夠考慮檢查傳遞的值是否是通過TDD的有效文件對象,如果我沒有TDD編碼就不會發生,但我不確定我是否正確使用unittest。 無法得到全局。 任何人都可以建議如何去做這個以及上面的代碼是否正確。

關於你的代碼

setUp和tearDown是可選的方法,可以幫助您設置和良好..拆除測試環境。 例如,它們通常用於創建和刪除臨時文件夾以存儲輸出或在測試期間設置(模擬)數據庫連接。

它們不應該用於測試任何功能。 因此,調用FileChunk對象不應該在setUp中發生。 在每個測試中,您希望在FileChunk中測試某些方法,理想情況下彼此獨立。 因此,您應該在每個新案例中調用FileChunk的新實例。 因此,在這種情況下, test_instance_variablestest_check_if_instance_variables_are_valid_file_objects

關於TDD

使用純TDD是一種思維方式的改變。 沒有任何簡單的教程可以幫助你; 因為有關如何使用TDD的全書已經寫好了。

但是,我可以為您提供一些指導。

  1. 確定您班級的公共界面。 外部類應該使用哪些方法? 應該輸入什么以及這些方法的輸出應該是什么?
  2. 確定方法的不同情況。 什么時候輸出真/假? 什么時候應該拋出異常?
  3. 根據您在2中找到的案例編寫測試用例。
  4. 為FileChunk編寫最基本的虛擬類,它基本上什么都不做,但確實具有單元測試正在測試的所有功能。 這樣可以運行所有測試,盡管它們可能會失敗。
  5. 開始改進FileChunk類,直到所有測試都通過。

在我看來,TDD的一個重要方面是你不能只是開始進行測試。 你應該真的知道這個類應該是什么樣的。

與TemporaryFile()不同,mkstemp()的用戶負責在完成臨時文件時刪除它。 - Python文檔

所以刪除tearDown中的文件是正確的解決方案。

我個人會做一些測試功能

def test_filename(self):
    self.assertEqual(self.fc_obj.filename, self.fname)
    self.assertIsTrue(os.path.isfile(self.fc_obj.filename))


def test_handle(self):
    self.assertIsNone(self.fc_obj.filehandle)

def open_file(self):
    # if you implemented an "open" method
    # you can use this method every time you test the opened file
    result = self.fc_obj.open()
    self.assertIsTrue(result, "File open failed")
    self.assertIsNotNone(self.fc_obj.filehandle)

# if you plan to implement a read and a write method you can add the following test
def test_read_write_file(self):
    self.open_file()
    random_data = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N))
    self.fc_obj.write(random_data)
    self.assertEqual(self.fc_obj.read(), random_data)

依此類推,為您計划實施的所有內容定義測試,實施並運行測試。 你的課程看起來一直很好,但是正如你可能已經看到的那樣,你應該盡可能地使測試具體化

self.assertEqual(self.fc_obj.filename, self.fname)

代替

self.assertIsNotNone(self.fc_obj.filename)

如果你想用你打開的FileChunk對象進行大量測試,你還可以添加第二個unittest.TestCase

class TestOpenFileChunk(unittest.TestCase);
    def setUp(self):
        self.fhandle, self.fname = mkstemp()
        self.fc_obj = FileChunk(filename=self.fname)
        self.fc_obj.open()

    def tearDown(self):
        # if you have this method
        self.fc_object.close()
        # and then
        try:
            os.remove(self.fname)
        except OSError as why:
            print(why)

    def test_read_write(self):
        #...

如果只想創建FileChunk對象,也可以使用setUpClasstearDownClass方法。

class TestOpenFileChunk(unittest.TestCase);
    @classmethod
    def setUpClass(cls):
        cls.fhandle, cls.fname = mkstemp()
        cls.fc_obj = FileChunk(filename=self.fname)
        cls.fc_obj.open()

    @classmethod
    def tearDownClass(cls):
        # if you have this method
        cls.fc_obj.close()
        # and then
        try:
            os.remove(cls.fname)
        except OSError as why:
            print(why)

並在測試方法中始終使用self.fc_obj。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM