py.test只找到5个项目,但我的班级有更多

时间:2013-10-15 11:21:51

标签: python unit-testing

我是新手测试所以也许我在这里做错了。

我刚开始为我的项目编写一些测试,一切顺利,直到py.test停止识别我写的更多测试。

我的意思是,有超过5个defs,但每次运行它时,它只检查5个项目。

知道为什么吗?

py.test

[itxaka@optimus tests]$ py.test-2.7 
test session starts 
platform linux2 -- Python 2.7.5 -- pytest-2.4.2
collected 5 items 

gitlab_test.py .....
5 passed in 3.37 seconds 

使用setup.py方法

[itxaka@optimus i-python-gitlab]$ python2 setup.py test
running test
running egg_info
writing requirements to pyapi_gitlab.egg-info/requires.txt
writing pyapi_gitlab.egg-info/PKG-INFO
writing top-level names to pyapi_gitlab.egg-info/top_level.txt
writing dependency_links to pyapi_gitlab.egg-info/dependency_links.txt
reading manifest file 'pyapi_gitlab.egg-info/SOURCES.txt'
writing manifest file 'pyapi_gitlab.egg-info/SOURCES.txt'
running build_ext
testbadlogin (tests.gitlab_test.GitlabTest) ... ok
testcurrentuser (tests.gitlab_test.GitlabTest) ... ok
testgetusers (tests.gitlab_test.GitlabTest) ... ok
testlogin (tests.gitlab_test.GitlabTest) ... ok
testsshkeys (tests.gitlab_test.GitlabTest) ... ok

----------------------------------------------------------------------
Ran 5 tests in 3.070s

OK

代码

import unittest
import gitlab

user = ""
password = ""
host = ""
key = ""
git = gitlab.Gitlab(host=host, user=user)


class GitlabTest(unittest.TestCase):
    def testlogin(self):
        """
        Test to see if login works with proper credentials
        """
        self.assertTrue(git.login(user=user, password=password))

    def testbadlogin(self):
        """
        Test to see if login fails with no credentials
        """
        self.assertFalse(git.login("", ""))

    def testgetusers(self):
        git.login(user=user, password=password)
        # get all users
        assert isinstance(git.getusers(), list)  # compatible with 2.6
        self.assertTrue(git.getusers())
        # get X pages
        assert isinstance(git.getusers(page=2), list)  # compatible with 2.6
        assert isinstance(git.getusers(per_page=4), list)  # compatible with 2.6
        self.assertEqual(git.getusers(page=800), list(""))  # check against empty list
        self.assertTrue(git.getusers(per_page=43))  # check against false

    def testcurrentuser(self):
        git.login(user=user, password=password)
        assert isinstance(git.currentuser(), dict)  # compatible with 2.6
        self.assertTrue(git.currentuser())

    def addremoveuserstest(self):
        git.login(user=user, password=password)
        newuser = git.createuser("Test", "test", "123456",
                                 "test@test.com", "skype",
                                 "linkedin", "twitter", "25",
                                 bio="bio")
        assert isinstance(newuser, dict)
        # this below doesn't really matter. Gilab always answers a 404
        self.assertTrue(git.edituser(newuser['id'], twitter="tweeeeet", skype="Microsoft", username="Changed"))
        self.assertTrue(git.deleteuser(newuser['id']))

    def testsshkeys(self):
        git.login(user=user, password=password)
        git.addsshkey(title="test key", key=key)
        assert isinstance(git.getsshkeys(), list)  # compatible with 2.6
        # pass the id of the first key
        assert isinstance(git.getsshkey(id_=git.getsshkeys()[0]['id']), dict)  # compatible with 2.6
        self.assertTrue(git.getsshkey(id_=git.getsshkeys()[0]['id']))
        self.assertTrue(git.deletesshkey(id_=git.getsshkeys()[0]['id']))
        self.assertTrue(git.addsshkey(title="test key", key=key))
        self.assertTrue(git.deletesshkey(id_=git.getsshkeys()[0]['id']))
        self.assertTrue(git.addsshkeyuser(id_=git.currentuser()['id'], title="tests key", key=key))
        self.assertTrue(git.deletesshkey(id_=git.getsshkeys()[0]['id']))

    def projecttest(self):
        git.login(user=user, password=password)
        # we won't test the creation of the project as there is no way of deleting it trougth the api
        # so we would end with a million test projects. Next Gitlab version allows to delete projects
        #self.assertTrue(git.createproject("Test-pyapy-gitlab"))
        assert isinstance(git.getprojects(), list)
        assert isinstance(git.getprojects(page=5), list)
        assert isinstance(git.getprojects(per_page=7), list)
        assert isinstance(git.getproject(git.getprojects()[0]['id']), dict)
        self.assertFalse(git.getproject("wrong"))
        assert isinstance(git.getprojectevents(git.getprojects()[0]['id']), list)
        assert isinstance(git.getprojectevents(git.getprojects()[0]['id'], page=3), list)
        assert isinstance(git.getprojectevents(git.getprojects()[0]['id'], per_page=4), list)

1 个答案:

答案 0 :(得分:2)

AFAIK,它只会将这些方法视为名称实际上以“test”开头的测试(通常,为了清楚起见,您会发现人们使用“test_”)。以这种精神重命名“projecttest”和“addremoveuserstest”应该有所帮助。 (有关此行为,另请参阅http://docs.python.org/dev/library/unittest.html。)