我在我的基本页面中写了几个函数,我正在试着在我的测试中调用它。但是,如果其中一个函数失败,则测试不会进一步进行。如何使用selenium实现这一点,因此测试将继续运行,并在稍后给出错误。
发布于 2015-11-27 01:45:01
在使用unittest.TestCase类或django.test.TestCase类进行的Python单元测试中,您应该为要测试的每个逻辑概念创建一个test_*函数。单元测试应该只因为一个原因而失败。为了一个原因有多种测试方法是可以的,但是测试应该只因为一个原因而失败。
测试类可以如下所示:
from django.test import LiveServerTestCase
from selenium import webdriver
class FooTestCase(LiveServerTestCase):
def setUp(self):
self.browser = webdriver.Firefox()
[...]
def tearDown(self):
self.browser.quit()
[...]
def test_thing_one(self):
self.browser.get(self.live_server_url)
self.assertIn('My Title', self.browser.title)
def test_thing_two(self):
[...]
self.assertTrue(foobar)在这个类中有两个test_*函数。当您运行这个测试用例并且都通过时,您会看到类似于:
..
-------------------------------
Ran 2 tests in 0.213s它告诉我们它做了两次测试。注意,它也有两个句点。每次测试一次。周期意味着通过了测试。都通过了。如果第一次测试失败,我们将看到:
F.
===============================
FAIL: test_thing_one (mytests.FooTestCase)
-------------------------------
Traceback (most recent call last):
# stack trace here
AssertionError: (rest of error)
-------------------------------
Ran 2 tests in 0.213s
FAILED (failures=1)这一次,其中一个时期已被字母F所取代,仍然有一个时期表示通过了测试。注意F优先。这意味着test_thing_one首先运行,失败,即使失败,test_thing_two仍然正常运行并通过。
在test_thing_one中,我们可以使用以下多个断言:
def test_thing_one(self):
[...]
self.assertIn(x, bar_list)
self.assertTrue(something)
self.assertIsInstance(a, b)现在,如果assertTrue失败,test_thing_one将停止,assertIsInstance永远不会被调用。这是正确的行为。如果仍然希望调用assertIsInstance (即使assertTrue失败),则应该创建一个新的test_*方法,并将assertIsInstance移动到该方法。
通常,每个assert*只有一个test_*。这将帮助您将其限制为只测试一个概念。如果这三个断言只测试一个概念,这是可以的,因为您将知道如何在代码中解决这一小问题。如果assertTrue测试一段代码,assertIsInstance测试另一段代码,那么它们应该位于两个单独的test_*方法中。
如果您有6个测试用例,并且没有看到6个周期或F的组合,那么其他一些异常正在发生。如果是这样的话,请用错误更新你的问题,这样我们就可以解决这个问题了。
发布于 2015-11-27 21:59:36
我采用了以下方法来实现这一目标。这样做对吗?
class mytest(BaseTestCase, unittest.TestCase):
def setUp(self):
super(mytest, self).setUp()
def test_one(self):
# content
def test_two(self):
# content
def test_three(self):
# content
def test_four(self):
# content
def test_five(self):
# content
def test_six(self):
# content
def tearDown(self):
super(mutest, self).tearDown()https://stackoverflow.com/questions/33948874
复制相似问题