Skip to main content
Spelling and grammar
Source Link
Toby Speight
  • 81.8k
  • 14
  • 101
  • 309

Starting with Uni-test Skeleton unit test for leetcode coding interviewsproblems

I tried to initiate myself with unit testtesting the simpliestsimplest way iI can. What do you think of this way to begin, is with? Is it a clean way to proceed? Isn'tAre there someany major caveats to avoid  ?

NB:

  • Since iI want to test a small script for each leetcode problem, i preferedI preferred to lethave the Test beTest inside the script to be tested (and not in a different file).
  • I decided to use ff as a global variable inside the main scope (iI could have defined it in the Test ClassTest as a class variable and use self.fself.f).
  • iI create a function test for each case.

class Solution(object):
    def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """


if __name__ == '__main__':
    import unittest
    f = Solution().islandPerimeter

    class Test(unittest.TestCase):
        
        def test_multiple_path(self):    
            self.assertEqual(f( [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]), 16)
         
        def test_one_square(self):
            self.assertEqual(f( [[1]]), 4)

    unittest.main()
import unittest

class Solution(object):
    def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """


if __name__ == '__main__':
    import unittest
    f = Solution().islandPerimeter

    class Test(unittest.TestCase):
        
        def test_multiple_path(self):    
            self.assertEqual(f( [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]), 16)
         
        def test_one_square(self):
            self.assertEqual(f( [[1]]), 4)

    unittest.main()

Starting with Uni-test for leetcode coding interviews

I tried to initiate myself with unit test the simpliest way i can. What do you think of this way to begin, is it a clean way to proceed? Isn't there some major caveats to avoid  ?

NB:

  • Since i want to test a small script for each leetcode problem, i prefered to let the Test be inside the script to be tested (and not in a different file)
  • I decided to use f as a global variable inside the main scope (i could have defined it in the Test Class as a class variable and use self.f)
  • i create a function test for each case

class Solution(object):
    def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """


if __name__ == '__main__':
    import unittest
    f = Solution().islandPerimeter

    class Test(unittest.TestCase):
        
        def test_multiple_path(self):    
            self.assertEqual(f( [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]), 16)
         
        def test_one_square(self):
            self.assertEqual(f( [[1]]), 4)

    unittest.main()

Skeleton unit test for leetcode coding problems

I tried to initiate myself with unit testing the simplest way I can. What do you think of this way to begin with? Is it a clean way to proceed? Are there any major caveats to avoid?

NB:

  • Since I want to test a small script for each leetcode problem, I preferred to have the Test inside the script to be tested (and not in a different file).
  • I decided to use f as a global variable inside the main scope (I could have defined it in Test as a class variable and use self.f).
  • I create a function test for each case.
import unittest

class Solution(object):
    def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """


if __name__ == '__main__':
    import unittest
    f = Solution().islandPerimeter

    class Test(unittest.TestCase):
        
        def test_multiple_path(self):    
            self.assertEqual(f( [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]), 16)
         
        def test_one_square(self):
            self.assertEqual(f( [[1]]), 4)

    unittest.main()
Source Link
curious
  • 313
  • 1
  • 5

Starting with Uni-test for leetcode coding interviews

I tried to initiate myself with unit test the simpliest way i can. What do you think of this way to begin, is it a clean way to proceed? Isn't there some major caveats to avoid ?

NB:

  • Since i want to test a small script for each leetcode problem, i prefered to let the Test be inside the script to be tested (and not in a different file)
  • I decided to use f as a global variable inside the main scope (i could have defined it in the Test Class as a class variable and use self.f)
  • i create a function test for each case

class Solution(object):
    def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """


if __name__ == '__main__':
    import unittest
    f = Solution().islandPerimeter

    class Test(unittest.TestCase):
        
        def test_multiple_path(self):    
            self.assertEqual(f( [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]), 16)
         
        def test_one_square(self):
            self.assertEqual(f( [[1]]), 4)

    unittest.main()