How to set the priority of the methods that are included in groups in testng.xml file? - testing

I have a class which has 2 methods. Each method is having a group associated.
Now I want to set my testng.xml file in such a way that when testcase TC1 executes, Method1() and then Method2() will be called and when testcase TC2 will be executed, Method2() and then Method1() will be executed.
Is there any way by which I can do that?

You can move your test case methods to a helper class and then simply create a test class for each test case, define your methods with their test priorities and/or dependencies, and call your shared test method code. e.g.:
public class TestCaseMethods {
public static void method1() { /*...*/ }
public static void method2() { /*...*/ }
}
public class TC1 {
#Test
public void method1() {
TestCaseMethods.method1();
}
#Test(dependsOnMethods = {"method1"})
public void method2() {
TestCaseMethods.method2();
}
}
public class TC2 {
#Test(dependsOnMethods = {"method2"})
public void method1() {
TestCaseMethods.method1();
}
#Test
public void method2() {
TestCaseMethods.method2();
}
}
You can also use inheritance if you prefer:
public class AbstractTC {
public void method1() { /*...*/ }
public void method2() { /*...*/ }
}
public class TC1 extends AbstractTC {
#Override
#Test
public void method1() {
super.method1();
}
#Override
#Test(dependsOnMethods = {"method1"})
public void method2() {
super.method2();
}
}
public class TC2 extends AbstractTC {
#Test(dependsOnMethods = {"method2"})
public void method1() {
super.method1();
}
#Test
public void method2() {
super.method2();
}
}

Related

Should service call another service or repository directly?

I am creating the WebApplication, with many layers (for now important are Model, Repository, BusinessLayer)
Having ClassService, ClassRepository and StudentService, StudentRepository, should ClassServiceMethod call methods from StudentService or StudentRepository?
Please provide as many arguments or additional links/blogs/informations as possible :)
Thanks in advance.
Here is my example code, some generics are added. The question is about GetClassAndBestStudent method:
Services - Business Layer
public class ClassService : BaseService<Class>, IClassService
{
IClassRepository classRepository; // Resolved by IoC, will be injected to BaseService
IStudentRepository studentRepository;
IStudentService studentService;
public virtual Class GetClassWithHighestNotes() { ... } // Do some stuff and call classRepository.GetClassWithHighestNotes()
public virtual Teacher GetTeachersByClass(int classId) { ... } // Do some stuff and call classRepository.GetTeachersByClass()
public virtual GetClassAndBestStudent(int classId)
{
// Question here: Which call is valid?
var best = studentRepository.GetStudentWithHighestNotes()
var best = studentService.GetStudentWithHighestNotes();
}
}
public class StudentService : BaseService<Student>, IStudentService
{
IStudentRepository studentRepository; // Resolved by IoC, will be injected to BaseService
public virtual IEnumerable<Student> GetStudentsByClass(int classId) { ... } // Do some stuff and call studentRepository.GetStudentsByClass()
public virtual Student GetStudentWithHighestNotes() { ... } // Do some stuff and call studentRepository.GetStudentWithHighestNotes()
}
// Abstract, generic CRUD service
public abstract class BaseService<T> : IBaseService<T> where T : MyBase
{
IRepository<T> repository;
public virtual IEnumerable<T> GetAll() { ... } // Do some stuff and call repository.GetAll()
public virtual T GetById(int id) { ... } // Do some stuff and call repository.GetById()
public virtual T Insert(T entity) { ... } // Do some stuff and call repository.Insert()
public virtual T Update(T entity) { ... } // Do some stuff and call repository.Update()
public virtual bool Delete(T entity) { ... } // Do some stuff and call repository.Delete()
public virtual bool Delete(int id) { ... } // Do some stuff and call repository.Delete()
}
Repositories - Data Layer
public class ClassRepository : BaseRepository<Class>, IClassRepository
{
public virtual Class GetClassWithHighestNotes() { ... }
public virtual Teacher GetTeachersByClass(int classId) { ... }
}
public class StudentRepository: BaseRepository<Student> IStudentRepository
{
public virtual IEnumerable<Student> GetStudentsByClass(int classId) { ... }
public virtual Student GetStudentWithHighestNotes() { ... }
}
// Abstract, generic CRUD repository
public abstract class BaseRepository<T> : IRepository<T> where T : MyBase
{
public virtual IEnumerable<T> GetAll() { ... }
public virtual T GetById(int id) { ... }
public virtual T Insert(T entity) { ... }
public virtual T Update(T entity) { ... }
public virtual bool Delete(T entity) { ... }
public virtual bool Delete(int id) { ... }
}
The best practice is calling StudentService from ClassServiceMethod. If the implementation in StudentRepository changes in the future, you can create another repository method like StudentRepositoryNew and utilize the same StudentService.

WCF possibly serialization related issue

On server I have the following class
public class A
{
string a1 {get; set ;}
string a2 {get; set;}
}
I have defined a service with the following operation contract
[OperationContract]
public list<A> GetAll()
{
return new List<A> {new A {a1="1", a2="2"}, new A{a1="3", a2="4"}};
}
in the reference there is defined a shallow copy of A in the following way
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="GetAll", Namespace="http://schemas.datacontract.org/2004/07/SomeModel")]
[System.SerializableAttribute()]
public partial class A: object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
[System.NonSerializedAttribute()]
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private string A1Field;
[System.Runtime.Serialization.OptionalFieldAttribute()]
private string A2Field;
[global::System.ComponentModel.BrowsableAttribute(false)]
public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
get {
return this.extensionDataField;
}
set {
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string A1{
get {
return this.A1Field;
}
set {
if ((object.ReferenceEquals(this.A1Field, value) != true)) {
this.A1Field= value;
this.RaisePropertyChanged("A1");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string A2{
get {
return this.A2Field;
}
set {
if ((object.ReferenceEquals(this.A2Field, value) != true)) {
this.A2Field= value;
this.RaisePropertyChanged("A2");
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
On the client I call the service.GetAll() and I use the shallow copy of A, defined in the proxy which defines my model for a view. the call is something similar to
ActionResult GetAll()
{
List<A> allAs = service.GetAll();
return new View (allAs);
}
However the list on the client is always emtpy. What is the problem?
You should define your data class, A as a datacontract:
[DataContract]
public class A
{
[DataMember]
public string a1 {get; set ;}
[DataMember]
public string a2 {get; set ;}
}
On the class you will need DataContract attribute from System.Runtime.Serialization.DataContractAttribute namespace.
Some thing like this
[DataContract]
public class A
{
[DataMember]
public string a1 {get; set ;} //This should be public
[DataMember]
public string a2 {get; set;}//This should be public
}
Read more on MSDN

NHibernate repository not compile

I don't understand why the code below not compile (on the QueryOver line), I tried wih NHibernate 3.1 and 3.2
public interface IRepository<T> where T : class
{
IQueryable<T> FindAll<T>();
void Save(T obj);
}
public class RepositoryBase<T> : IRepository<T> where T : class
{
protected ISession _session = null;
public RepositoryBase(ISession session)
{
_session = session;
}
public void Save(T obj)
{
_session.Save(obj);
}
public IQueryable<T> FindAll<T>()
{
- return _session.QueryOver<T>().List<T>().AsQueryable();
}
}
Error :
You do not need <T> in FindAll declarations because they already declared at the class level. You may also be missing some using statements. And there is a dash ( - ) in the QueryOver line. Following should compile in .NET 3.5 project:
using System.Linq;
using NHibernate;
public interface IRepository<T> where T : class {
IQueryable<T> FindAll();
void Save(T obj);
}
public class RepositoryBase<T> : IRepository<T> where T : class {
protected ISession _session = null;
public RepositoryBase(ISession session) {
_session = session;
}
public void Save(T obj) {
_session.Save(obj);
}
public IQueryable<T> FindAll() {
return _session.QueryOver<T>().List<T>().AsQueryable();
}
}

OperationBehaviorAttribute inheritance

I have base class for my services. Attribute OperationBehavior doesn't apply if it defined in base class and I override method in derived class. Of cause, I can duplicate code, but maybe there is other way...
[ServiceContract]
public interface IMyService
{
[OperationContract]
void DoWork();
}
public class MyServiceBase
{
[OperationBehavior(TransactionScopeRequired = true)]
public virtual void DoWork()
{
}
}
public class MyService : MyServiceBase, IMyService
{
public override void DoWork()
{
//No Transaction, because attribute OperationBehavior doesn't apply.
}
}
You will need to do something like the following:
public class MyServiceBase
{
[OperationBehavior(TransactionScopeRequired = true)]
public void DoWork()
{
DoWorkImpl();
}
protected virtual DoWorkImpl()
{
}
}
public class MyService : MyServiceBase, IMyService
{
protected override void DoWorkImpl()
{
//Should have a Tx here now
}
}

Is an interface or an abstract class or both better in the following situation?

Assume I have some program that pulls airline data. Each airline data uses a different system in which I need to pull the data (xml) from. Because of the difference in each system, basically the xml being different and maybe some propeterties, I decide to implement an interface to implement for each airline system. What I find though is that each airline, although different in many aspects, also contains many similarities, so if I do it the interface way first, I might have to do something like this:
public interface IAirlineSystem {
public void PullData();
}
Now for each airline system (I am making these names up), I might do something like:
public class Oberon : IAirlineSystem {
string Name {get;set;}
int Code {get;set;}
public void Initialize()
{
//initialize code here
}
public void PullData()
{
//code here
}
}
The above is just one airline system, but imagine I have more (like +10), another one might be:
public class AirSys: IAirlineSystem {
string Name {get;set;}
int Code {get;set;}
public void Initialize()
{
//initialize code here
}
public void PullData()
{
//code here
}
}
In the code above:
The name and code properties are unique to each Airline system. Initialize contains the exact same code for each implementation and PullData is unique to each system. Because of duplicates in the classes, is this where I could use an abstract class to hold the code of the Initialize method. I have heard that it is good practice to actually mix interfaces and abstract classes, what would be an example of this using my airline system example?
Another thing that comes up is that lets assume that I have another method in the interface called ValidateData, but not all airline systems need to call it, but if I put it in an interface, I would need to implement an empty method if it is not needed. Is there a way I can prevent from having to do this. Is this another reason for using an abstract class, maybe with a virtual method, so it can be overridden as needed. If I made ValidateData an abstract method, it would still need to be implemented, right?
Your explanation seems to point towards using an abstract class. You could have an abstract class that would have an implementation of the Initialize method, and you would not need to repeat it in the Oberon and AirSys subclasses. PullData could be an abstract method that would then get implemented in the separate subclasses. And yes, only the class that requires the ValidateData method would have it implemented.
Example would be:
public abstract class AirlineSystemBase
{
string Name { get; set; }
int Code { get; set; }
public void Initialize()
{
//code goes here
}
public abstract void PullData();
}
public class Oberon : AirlineSystemBase
{
public override void PullData()
{
//code goes here
}
}
public class AirSys : AirlineSystemBase
{
/// <summary>
/// that is if say only AirSys class had a need for this ValidateData() method
/// </summary>
public void ValidateData()
{
//code goes here
}
public override void PullData()
{
//code goes here
}
}
OR if you want to use both interface and abstract class:
public interface IAirlineSystem
{
void PullData();
void ValidateData();
}
public abstract class AirlineSystemBase : IAirlineSystem
{
string Name { get; set; }
int Code { get; set; }
public void Initialize()
{
//code goes here
}
public abstract void PullData();
public virtual void ValidateData()
{
//code goes here
}
}
public class Oberon : AirlineSystemBase
{
public override void PullData()
{
//code goes here
}
}
public class AirSys : AirlineSystemBase
{
/// <summary>
/// that is if say only AirSys class had a need for this ValidateData() method
/// </summary>
public override void ValidateData()
{
//code goes here
}
public override void PullData()
{
//code goes here
}
}
An example, I would implement something like:
public interface IAirlineSystem {
public void PullData();
public void ValidateData();
}
public abstract class BaseAirlineSystem : IAirlineSystem {
string Name {get;set;}
int Code {get;set;}
public void Initialize()
{
// common initialize code here
}
public virtual void ValidateData()
{
//empty, override when necessary
}
}
And a sample implementation:
public class AirSys: BaseAirlineSystem{
public void PullData()
{
//code here
}
}