Selenium3.4.0-Python3.6.1 : In Selenium-Python binding using unittest how do I decide when to use self.assertIn or assert

后端 未结 2 791
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 02:28

I am working with Selenium 3.4.0 with Python 3.6.1. I have written a script following the Python documentation through unittest module which is a built-in Pytho

相关标签:
2条回答
  • 2021-01-18 02:39

    Looking at the Python unittest documentation and also recalling from the bunch of Django unittests that I once had to do here, are my findings.

    USE CASE:

    First thing that is the most simple thing and in my opinion the biggest difference between the two, is the cases where you can use each command. They are both interchangeably usable in the case of a test class, however, in order to use the assertIn command, you need to import the unittest library. So, say I want to know if h is in hello. A simple way to do it through the assertIn command is:

    class MyTestCase(unittest.TestCase):
        def is_h_in_hello(self):
            self.assertIn("h", "hello")
    

    and then I need to run tests, that is through unittest.main() in this example, in order to get my answer. But using the assert command, it is much easier to see if h is in hello. Which is very simply done like so:

    assert "h" in "hello"
    

    But essentially, both will give me the same answer. However, what distinguishes the two methods is the simplicity of use in the second method.

    RESULTS:

    The second difference I found was the readability of the results on Python Shell. The unittest library is designed so that the commands are very specific. So if a test fails, you will receive a very clear message saying what went wrong. Say now you want to see if b is in hello. Doing it through the class method (simply changing "h" to "b"), the message we get after running the test is:

    AssertionError: 'b' not found in 'hello'
    
    ----------------------------------------------------------------------
    Ran 1 test in 0.038s
    
    FAILED (failures=1)
    

    So it very clearly tells you: 'b' not found in 'hello', which makes it very convenient to see what exactly is the problem. But say you do the same process through the assert command. The error message generated is something like:

    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        assert "b" in "hello"
    AssertionError
    

    Although it tells you the error type (AssertionError), and the traceback, it does not specifically tell you that "b" is NOT in "hello". In this simple case, it is very easy to look at the traceback and say oh, there's no b in hello! However in more complicated cases, it might be tricky to see why this error message was generated.

    Overall, the two methods are very similar and will get you the result you want, but essentially it comes down to the little differences here and there, having less lines of code and how straight-forward the Shell messages are.

    0 讨论(0)
  • 2021-01-18 02:52

    In my opinion, assert is the built-in keyword in python which tells internal state of program. In other words, with the assert keyword, you can tell your code behavior. In your case, you should not use assert in testcase.

    0 讨论(0)
提交回复
热议问题