Selenium3.4.0-Python3.6.1:在使用unittest的Selenium-Python绑定中,我如何决定何时使用self.assertIn或assert [英] Selenium3.4.0-Python3.6.1 : In Selenium-Python binding using unittest how do I decide when to use self.assertIn or assert

查看:163
本文介绍了Selenium3.4.0-Python3.6.1:在使用unittest的Selenium-Python绑定中,我如何决定何时使用self.assertIn或assert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Python 3.6.1的Selenium 3.4.0.我通过unittest模块按照Python文档编写了脚本,该模块是基于Java JUnit的内置Python,在Windows 8 Pro计算机,64位OS,x-64处理器上使用geckodriver 0.16.1和Mozilla Firefox 57.0 .在我的测试方法test_search_in_python_org()中,以下几行效果很好:

    def test_search_in_python_org(self):
      driver = self.driver
      driver.get("http://www.python.org")
      self.assertIn("Python", driver.title)
      elem = driver.find_element_by_name("q")
      elem.send_keys("pycon")
      elem.send_keys(Keys.RETURN)
      assert "No results found." not in driver.page_source

当我断言页面标题"时,我正在使用:self.assertIn("Python", driver.title)

但是,当我断言一个字符串(我的假设)时,我在页面源中使用的是:assert "No results found." not in driver.page_source

我的问题是什么因素/条件决定我应该使用self.assertIn还是简单地使用assert?

任何建议或指示都会有所帮助.

解决方案

查看Python unittest

When I am asserting the "page title" I am using: self.assertIn("Python", driver.title)

But, when I am asserting a string (my assumption), within the page source I am using: assert "No results found." not in driver.page_source

My question is what are the factors/conditions which decides whether I should use self.assertIn or simply assert?

Any suggestions or pointers will be helpful.

解决方案

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.

这篇关于Selenium3.4.0-Python3.6.1:在使用unittest的Selenium-Python绑定中,我如何决定何时使用self.assertIn或assert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆