1、问题背景 用户在编写测试时遇到问题,py.test只发现5个测试项,但实际上测试类中定义了更多测试项。用户猜测可能是自己做错了什么,因此寻求帮助。
2、解决方案
用户在测试方法的命名上存在问题,导致py.test无法识别。py.test只识别以“test”或“test_”开头的方法为测试方法,而用户定义的方法中有一些没有以“test”开头,因此py.test无法识别这些方法。
代码示例:
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