Python存储库组织w.r.t.测试和PyDev

时间:2017-10-27 10:15:49

标签: python unit-testing pydev

我正在尝试为某些代码设置Python存储库。我已经阅读了Structuring Your Project教程并按照建议设置了所有内容。具体来说,我有以下目录结构:

<html>
	<head>
		<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
		<link rel="stylesheet" href="css/styles.css" />
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
		<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>		
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
		
	</head>
	<body>
		<div class="container-fluid">
			<div class="row">
				<div class="col-lg-8 col-md-8 hidden-sm-down" style="background-color:#4E5B5C; color:#fff"><span><small>Heading heading heading 1</small></span></div>
				<div class="col-lg-2 col-md-2 hidden-sm-down" style="background-color:#626F70; color:#fff"><span>Heading 2</span></div>
				<div class="col-lg-2 col-md-2 hidden-sm-down" style="background-color:#475859; color:#fff"><span>Heading 3</span></div>
			</div>
			<div class="row" style="background-color: #2B3536;">
				<div class="col-lg-2 col-md-2 col-sm-2 col-2 logo-div align-self-center"><img class="img-fluid" src="https://dummyimage.com/127x50/2b3536/fff"></div>
				<div class="col-lg-7 col-md-8 col-sm-8 col-8">
					<nav class="navbar navbar-wrapper navbar-fixed-top navbar-toggleable-sm navbar-expand-sm">
						<div class="container">
						  <a href="#" class="hidden-md-up mr-3" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false">
							<i class="fa fa-bars fa-lg text-white"></i>
						  </a>
						  <!--<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
							<span class="navbar-toggler-icon"></span>
						  </button>-->
						  <div class="collapse navbar-collapse pl-sm-2" id="navbarNavDropdown">
							<ul class="navbar-nav header-nav">
							  <li class="nav-item">
								<a class="nav-link" href="#">Header menu item 1</a>
							  </li>
							  <li class="nav-item">
								<a class="nav-link" href="#">Header menu item 2</a>
							  </li>
							  <li class="nav-item">
								<a class="nav-link" href="#">Header menu item 3</a>
							  </li>
							  <li class="nav-item">
								<a class="nav-link" href="#">Header menu item 4</a>
							  </li>
							  <li class="nav-item">
								<a class="nav-link" href="#">Header menu item 5</a>
							  </li>
							  <li class="nav-item">
								<a class="nav-link" href="#">Header menu item 6</a>
							  </li>
							  <!--<li class="nav-item dropdown">
								<a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
								  Dropdown link
								</a>
								<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
								  <a class="dropdown-item" href="#">Action</a>
								  <a class="dropdown-item" href="#">Another action</a>
								  <a class="dropdown-item" href="#">Something else here</a>
								</div>
							  </li>-->
							</ul>
						  </div>
						</div>
					</nav>
				</div>
				<div class="col-lg-3 col-md-2 col-sm-2 col-2 align-self-center">
					<div class="input-group">
					  <input class="form-control hidden-md-down"
							 placeholder="I'm looking for">
					  <div class="input-group-addon top-search-addon"><i class="fa fa-search fa-search-white"></i></div>
					</div>
				</div>
			</div>
			<div class="row">
				<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
				  <div class="carousel-inner" role="listbox">
					<div class="carousel-item active">
					  <img class="d-block img-fluid" src="https://dummyimage.com/1290x672/ccc/000" alt="First slide">
					  <!--<div class="carousel-caption d-none d-md-block">
						<h3>...</h3>
						<p>...</p>
					  </div>-->
					</div>
					<div class="carousel-item">
					  <img class="d-block img-fluid" src="https://dummyimage.com/1290x672/ccc/000" alt="Second slide">
					</div>
					<div class="carousel-item">
					  <img class="d-block img-fluid" src="https://dummyimage.com/1290x672/ccc/000" alt="Third slide">
					</div>
				  </div>
				  <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
					<span class="carousel-control-prev-icon" aria-hidden="true"></span>
					<span class="sr-only">Previous</span>
				  </a>
				  <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
					<span class="carousel-control-next-icon" aria-hidden="true"></span>
					<span class="sr-only">Next</span>
				  </a>
				</div>
			</div>
		</div>
		
	</body>
</html>

repo_root/ some_module.py tests/ context.py test_some_module.py 文件完全包含教程建议的内容:

context.py

import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) import some_module 文件导入test_some_module.py,如下所示:

some_module

而不是

from context import some_module

正如网站建议的那样。

我正在使用PyDev并通过右键单击测试文件并选择from .context import some_module 来运行单元测试。这会导致测试在Run As/unittest目录中运行,其中tests可见且导入成功。 PyDev显示context。我猜这是因为它从项目根运行解析器。如果我将Unresolved import添加到tests,它仍然无效。只有当我在PYTHONPATH中添加__init__.py文件时,PyDev才会停止显示错误,因为它将tests视为库。

设置存储库的正确方法是什么?是否应该只在tests目录中调用测试,还是应该从存储库根目录开始测试?

1 个答案:

答案 0 :(得分:1)

如果你的包中没有__init__个文件(即使它是测试版),PyDev也不会很好用,所以,我的建议是在那里添加__init__