mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2026-02-17 14:28:25 +08:00
Test project: Enable netcore3.1 testing, switch win64 Snap7 and use custom port.
- Add netcoreapp3.1 target framework, allowing this to run on linux and macos as well. - Switch windows snap7 DLL to 64bit version 1.4.2. This also improves UnitTest stability (reduces false positives) on the CI (including appveyor) - Changing the port used for S7NetTests when communicating with Snap7 to a value > 1000 allows tests to run on Linux without elevated privileges.
This commit is contained in:
@@ -29,7 +29,7 @@ namespace S7.Net.UnitTest.Helpers
|
||||
Console.WriteLine(Server.EventText(ref Event));
|
||||
}
|
||||
|
||||
public static void Start()
|
||||
public static void Start(short port)
|
||||
{
|
||||
Server = new S7Server();
|
||||
// Share some resources with our virtual PLC
|
||||
@@ -59,7 +59,14 @@ namespace S7.Net.UnitTest.Helpers
|
||||
// Start the server onto the default adapter.
|
||||
// To select an adapter we have to use Server->StartTo("192.168.x.y").
|
||||
// Start() is the same of StartTo("0.0.0.0");
|
||||
|
||||
Server.SetParam(S7Consts.p_u16_LocalPort, ref port);
|
||||
|
||||
int Error = Server.Start();
|
||||
if (Error != 0)
|
||||
{
|
||||
throw new Exception($"Error starting Snap7 server: {Server.ErrorText(Error)}");
|
||||
}
|
||||
//if (Error == 0)
|
||||
//{
|
||||
// // Now the server is running ... wait a key to terminate
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net452</TargetFrameworks>
|
||||
<TargetFrameworks>net452;netcoreapp3.1</TargetFrameworks>
|
||||
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<AssemblyOriginatorKeyFile>Properties\S7.Net.snk</AssemblyOriginatorKeyFile>
|
||||
<IsPackable>false</IsPackable>
|
||||
<Copyright>Copyright © 2014</Copyright>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="GitHubActionsTestLogger" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
|
||||
@@ -23,14 +25,13 @@
|
||||
<ProjectReference Include="..\S7.Net\S7.Net.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ItemGroup Condition="'$(TargetFramework)'=='net452'">
|
||||
<Reference Include="System.ServiceProcess" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="snap7.dll">
|
||||
<None Update="runtimes\win-x64\native\snap7.dll" Link="snap7.dll">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -6,10 +6,12 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using S7.Net;
|
||||
using S7.Net.UnitTest.Helpers;
|
||||
using S7.Net.UnitTest;
|
||||
using System.ServiceProcess;
|
||||
using S7.Net.Types;
|
||||
using S7.UnitTest.Helpers;
|
||||
using System.Threading.Tasks;
|
||||
#if NETFRAMEWORK
|
||||
using System.ServiceProcess;
|
||||
#endif
|
||||
using System.Threading;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -6,9 +6,11 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using S7.Net;
|
||||
using S7.Net.UnitTest.Helpers;
|
||||
using S7.Net.UnitTest;
|
||||
using System.ServiceProcess;
|
||||
using S7.Net.Types;
|
||||
using S7.UnitTest.Helpers;
|
||||
#if NETFRAMEWORK
|
||||
using System.ServiceProcess;
|
||||
#endif
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -41,6 +43,7 @@ namespace S7.Net.UnitTest
|
||||
#region Constants
|
||||
const int DB2 = 2;
|
||||
const int DB4 = 4;
|
||||
const short TestServerPort = 31122;
|
||||
#endregion
|
||||
|
||||
#region Private fields
|
||||
@@ -53,16 +56,21 @@ namespace S7.Net.UnitTest
|
||||
/// </summary>
|
||||
public S7NetTests()
|
||||
{
|
||||
plc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2);
|
||||
plc = CreatePlc();
|
||||
//ConsoleManager.Show();
|
||||
ShutDownServiceS7oiehsx64();
|
||||
|
||||
}
|
||||
|
||||
private static Plc CreatePlc()
|
||||
{
|
||||
return new Plc(CpuType.S7300, "localhost", TestServerPort, 0, 2);
|
||||
}
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
S7TestServer.Start();
|
||||
S7TestServer.Start(TestServerPort);
|
||||
plc.Open();
|
||||
}
|
||||
|
||||
@@ -931,9 +939,9 @@ namespace S7.Net.UnitTest
|
||||
{
|
||||
plc.Close();
|
||||
S7TestServer.Stop();
|
||||
S7TestServer.Start();
|
||||
S7TestServer.Start(TestServerPort);
|
||||
|
||||
var reachablePlc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2);
|
||||
var reachablePlc = CreatePlc();
|
||||
Assert.IsTrue(reachablePlc.IsAvailable);
|
||||
}
|
||||
|
||||
@@ -1028,6 +1036,7 @@ namespace S7.Net.UnitTest
|
||||
#region Private methods
|
||||
private static void ShutDownServiceS7oiehsx64()
|
||||
{
|
||||
#if NETFRAMEWORK
|
||||
ServiceController[] services = ServiceController.GetServices();
|
||||
var service = services.FirstOrDefault(s => s.ServiceName == "s7oiehsx64");
|
||||
if (service != null)
|
||||
@@ -1037,6 +1046,7 @@ namespace S7.Net.UnitTest
|
||||
service.Stop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#region IDisposable Support
|
||||
|
||||
@@ -36,11 +36,8 @@ namespace Snap7
|
||||
|
||||
public class S7Consts
|
||||
{
|
||||
#if __MonoCS__ // Assuming that we are using Unix release of Mono (otherwise modify it)
|
||||
public const string Snap7LibName = "libsnap7.so";
|
||||
#else
|
||||
public const string Snap7LibName = "snap7.dll";
|
||||
#endif
|
||||
public const string Snap7LibName = "snap7";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// PARAMS LIST
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
BIN
S7.Net.UnitTest/runtimes/win-x64/native/snap7.dll
Normal file
BIN
S7.Net.UnitTest/runtimes/win-x64/native/snap7.dll
Normal file
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user