Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am having an hard time learning test driven development.

I am writing a class which would take either a filename or file description and size an input and return the size-chunk of data from the file.

While starting out with the tests first all I could come up was to test if the arguments passed were not none and to check if arguments are valid fileobjects.

All i could muster up was below code, am I use the setUp and tearDown methods as I should or it is completely wrong? I am creating a temporary file and instance of class defined to read in the setUp() and am I supposed to remove the object someway in tearDown()

Below is the code

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()

I went through multiple TDD questions on stackoverflow and suggested tutorials but still it seems following a TDD tutorial is very interesting but actually doing TDD is very difficult. I can actually think of what I want to do in ReadChunk class but just cannot get my head around actually find the tests first and then writing the code. I was able to think to check whether the passed values are valid file object through TDD which wouldn't have occurred if I were coding without TDD, but I am not sure if I am using the unittest correctly. Cannot get the big picture. Could anyone please suggest how to go about this and whether the above code snipped is correct.

like image 742
anukalp Avatar asked Jun 16 '14 09:06

anukalp


People also ask

How do I use Python setUp and tearDown?

In general you add all prerequisite steps to setUp and all clean-up steps to tearDown. You can read more with examples here. When a setUp() method is defined, the test runner will run that method prior to each test. Likewise, if a tearDown() method is defined, the test runner will invoke that method after each test.

Where will you use setUp () and tearDown () methods?

Prepare and Tear Down State for a Test Class XCTest runs setUp() once before the test class begins. If you need to clean up temporary files or capture any data that you want to analyze after the test class is complete, use the tearDown class method on XCTestCase .

What are setUp () and tearDown () methods in mobile app testing?

For that it has two important methods, setUp() and tearDown() . setUp() — This method is called before the invocation of each test method in the given class. tearDown() — This method is called after the invocation of each test method in given class.

What is tear down in Python?

Sometimes we want to prepare a context for each test to be run under. The setUp method is run prior to each test in the class. tearDown is run at the end of every test. These methods are optional.


1 Answers

About your code

setUp and tearDown are optional methods to help you setup and well.. tear down the testing environment. They are commonly used to, for example, create and remove temporary folders to store output or setup a (mocked) database-connection during tests.

They should not be used test any functionality. So invoking the FileChunk-object should not happen in the setUp. In each test you want to test a certain case of the methods in FileChunk, ideally independently of each other. Therefore, you should invoke a new instance of FileChunk in every new case. So in this case in both test_instance_variables and test_check_if_instance_variables_are_valid_file_objects.

About TDD

To use pure TDD is kind of a change in mindset. There aren't any simple tutorials to you help on that; as entire books have been written about how to use TDD.

However, I can provide you with some guidelines.

  1. Identify the public interface of your class. Which methods should external classes be able to use? What should be the input and what should be the output of these methods?
  2. Identify the different cases of the method. When should it output true/false? When should it throw an exception?
  3. Write the test-cases based on the cases you have found in 2.
  4. Write the most basic dummy class for FileChunk, which essentially does nothing, but does have all functionality the unit-tests are testing. This way all tests can be run, although they will probably fail.
  5. Start improving the FileChunk-class until all tests pass.

An important aspect of TDD, in my opinion, is that you can't just start making tests. You should really know what the class should look like.

like image 93
Erwin Avatar answered Nov 14 '22 23:11

Erwin