简体   繁体   English

我正在使用unittest.TestCase如果我有多个测试用例,它将无法运行。 仅运行1个测试用例

[英]I am using unittest.TestCase if i have more than 1 test case it will not run. Only 1 test case runs

I am automating our website. 我正在自动化我们的网站。 I am using Python, Webdriver, unittest.TestCase . 我正在使用Python,Webdriver, unittest.TestCase

I had 1 test method defined in my Login Page class which is to test a valid user log in. That works fine. 我在Login Page类中定义了1个测试方法,该方法用于测试有效的用户登录。

Now i am adding a 2nd test method called test_login_invalid_user to test an invalid user log in. I am just browsing to the log in page to start with to see if the 2nd test method gets called when i run my test. 现在,我添加了一个名为test_login_invalid_user的第二种测试方法来测试无效的用户登录。我只是浏览到登录页面以开始查看我运行测试时是否调用了第二种测试方法。

When i run my code i get the following error: 当我运行代码时,出现以下错误:

Traceback (most recent call last):
File "C:\Users\riaz.ladhani\PycharmProjects\Selenium Webdriver\ClearCore  \LoginPage_TestCase.py", line 40, in test_login_invalid_user
login_page = page.login(self.driver)
AttributeError: 'module' object has no attribute 'login'

Can I not have more than 1 test method in my unit test? 单元测试中可以有不止一种测试方法吗? I am doing it the wrong way? 我做错了吗?

My unit test class code snippet is as follows: 我的单元测试课程代码片段如下:

import unittest
from selenium import webdriver
import page
import time

class LoginPage_TestCase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Ie("C:\QA\Automation\Python_projects\Selenium  Webdriver\IEDriverServer_Win32_2.45.0\IEDriverServer.exe")
        self.driver.get("http://riaz-pc.company.local:8080/clearcore")

    def test_login_valid_user(self):
        login_page = page.LoginPage(self.driver)
        login_page.userLogin_valid()
        login_page.isAdministration_present()
        assert login_page.isAdministration_present(), "Administration not found"
        if login_page.isAdministration_present() == "true":
            print "test login with a valid user passed"
        else:
            print "test login with a valid user failed"

    def test_login_invalid_user(self):
        print "test_login_invalid_user"
        login_page = page.login(self.driver)

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

From what I understand, you meant to define a new LoginPage page object instance instead: 据我了解,您的意思是定义一个新的LoginPage页面对象实例:

def test_login_invalid_user(self):
    print "test_login_invalid_user"
    login_page = page.LoginPage(self.driver)

If this is the case, to avoid violating the "DRY" principle and to improve readability, instantiate LoginPage in setUp() : 如果是这种情况,为避免违反“ DRY”原理并提高可读性,请在setUp()实例化LoginPage

class LoginPage_TestCase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Ie("C:\QA\Automation\Python_projects\Selenium  Webdriver\IEDriverServer_Win32_2.45.0\IEDriverServer.exe")
        self.driver.get("http://riaz-pc.company.local:8080/clearcore")

        self.login_page = page.LoginPage(self.driver)

    def test_login_valid_user(self):
        self.login_page.userLogin_valid()
        self.login_page.isAdministration_present()
        assert self.login_page.isAdministration_present(), "Administration not found"
        if self.login_page.isAdministration_present() == "true":
            print "test login with a valid user passed"
        else:
            print "test login with a valid user failed"

    def test_login_invalid_user(self):
        print "test_login_invalid_user"
        # use self.login_page here

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python Webdriver中运行多个测试用例。 我的测试用例类中只有1个可以运行 - How do i run more than 1 Test Case in Python Webdriver. Only 1 of my test case class runs 如何在PyCharm中使用py.test运行unittest.TestCase子类? - How do I run unittest.TestCase subclasses with py.test in PyCharm? Selenium python如何使用来自unittest.TestCase的assertEqual方法“没有这样的测试方法 <class 'unittest.case.TestCase'> ” - Selenium python how to use assertEqual method from unittest.TestCase “No such test method in <class 'unittest.case.TestCase'>” unittest.TestCase的setUp方法是否知道当前测试用例? - Does setUp method from unittest.TestCase know the current test case? Django 测试运行器不尊重 unittest.TestCase? - Django test runner not respecting unittest.TestCase? 使用unittest.TestCase的运行方法 - Using run method of unittest.TestCase 是否可以从 python unittest.TestCase 运行单个测试方法并引用该方法? - Is it possible to run a single test method from a python unittest.TestCase with a reference to the method? 从unittest.TestCase继承以实现非测试功能 - Inheriting from unittest.TestCase for non-test functionality 使用unittest.TestCase子类Python的方法调用测试方法 - Invoking Test Methods with a Method for a unittest.TestCase Subclass Python 通过命令行从 unittest.TestCase 运行单个测试 - Running a single test from unittest.TestCase via the command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM